...

View Full Version : Resolved PHP and Javascript



InterbredMonkey
01-27-2010, 12:54 AM
I have a wrote some php which has some value and I wanted to pass a javascript function the php value while calling the javascript from php.

I know I can call the function of the javascript from an echo in the php however I want to call the function more than once throughout the whole of the php and pass different variables so I wrote some php variables to contain the first bit and the last bit of the javascript for example:


$js = '<script language = Javascript>';
$js2 = '</script>';

then in invoked the function like so:


echo($js. 'setCookie("name_iARC",' .$info['fName'].')' .$js2);

$info is the variable from the array that I require to pass, this parses fine but it does not work. Any ideas why? have I done something wrong somewhere?

Thanks.

Len Whistler
01-27-2010, 01:30 AM
This works.


<?php
$f_name = "John Smith";
?>

<script language="JavaScript">
function setCookie(c_name,value) {
document.cookie=c_name+ "=" +escape(value);
}

var php_variable="<?php echo "$f_name"; ?>";

setCookie('cookie_name',php_variable);
</script>



-------------------

InterbredMonkey
01-27-2010, 04:43 PM
Thank you Len,

I have sorted it now, I wrote all the code for the javascript into the PHP echo. I forgot I needed to break out of the javascript and add the php variable.

Done like so if anyone else gets stuck...


echo("<script language = Javascript>setCookie('username', \"$user\")</script>");

Notice you have to wrap the variable..


" \"$user\"

Sorted.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum