Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.
Seems people just won't stop asking for this =)
Ok. Here is the basic example on how to pass some php value to javascript. Currently it works only with strings and arrays (but arrays can have arbitrary number of dimensions). Use it like I did in testing section.
//------------------------------------------------//
// PHP2JS example //
// //
// Author: Weirdan //
// //
// You can use this example for whatever purpose. //
// Feel free to modify and improve it in any way. //
// NO WARRANTIES, of course =) //
// It would be nice to mention my name if you use //
// this snippet. //
//------------------------------------------------//
/**
* php2js - convert php value to JavaScript code string
*
* @param mixed $php value to convert
* @param string $js_name how we should name the variable
* in js output
*
* @return string js code. You can echo it to
* browser.
*/
function php2js($php,$js_name='') {
$js = '';
$top_level = (bool) strlen($js_name);
if($top_level) {
$js = 'var ' . $js_name . ' = ';
}
switch ( strtolower( gettype($php) ) ) {
case 'array':
$js .= 'new Array( ';
foreach(array_values($php) as $pos => $php_val) {
if($pos > 0)
$js .= ', ';
$js .= php2js($php_val);
}
$js .= ' ) ';
break;
default: // treat all other types as strings
$js .= '''' . strval($php) . '''';
break;
}
return $js . ($top_level ? ';' : '');
}
// testing
$array = array(
"something" => array("something else", "someone else"),
"asd"
);
echo php2js($array,'some_array');
Hey I read ur article .
was goooood enuf .
but my problem is just the reverse.
i want to input some values form user thru js
by using prompts,confirms etc. and want to use those in PHP
PHP processes everything server-side. JavaScript process everything client-side. So anything you do in JavaScript must be posted back to the server so that PHP can process that data and return back to the client.
n00b Saibot wrote:
but my problem is just the reverse.
i want to input some values form user thru js
by using prompts,confirms etc. and want to use those in PHP
Basically, there's two methods for js code to communicate with php:
Simple: assign your js values to hidden fields of a form. When user submits the form, the values will get transmitted (either as POST or GET vars) to your php script. Alternatively you could construct the link with all the vars set as you wish and then force the user's browser to visit the link by assigning it to document.location.href.
Advanced, web-service like: check out JPSPAN docs.
Last edited by Weirdan on Tue Jul 18, 2006 10:45 am, edited 1 time in total.
there's one question (more of a bit like poll question, he he): D'ya think that if we look at features provided in JS and PHP and compare them, we find that features-wise JS is may be equally/more powerful than PHP.
Again, if that is a silly thing 2 ask please 4give me