HAN! Posted October 23, 2007 Share Posted October 23, 2007 how to use php variables in javascript, i want to transfer php variables into javascript ones and then use them in javascript, thanks for any help. Quote Link to comment Share on other sites More sharing options...
Spyw Posted October 23, 2007 Share Posted October 23, 2007 You could always have <input type="hidden" name="name" value="<?php echo $variable; ?>"/> And then use var variable = document.form.name.value; That seems about the easiest way to do it, but I'm not sure if that could cause any security problems. I'd wait for somebody else's opinion on this. This is just an idea. Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted October 23, 2007 Share Posted October 23, 2007 you must use single quots jsfunc('<?php $phpvar; ?>') Quote Link to comment Share on other sites More sharing options...
nogray Posted October 23, 2007 Share Posted October 23, 2007 make sure your output is in the correct syntax and you should be find. <script language="javascript"> // if the variable might contain " mark var something = "<?php echo str_replace("\"", "\\\"", $var_goes_here);?>"; // if the variable is clean var something = "<?php echo $var_goes_here;?>"; // if the variable is a number var something = <?php echo $var_goes_here;?>; </script> When you view the page, view the source code to make sure it's correct Quote Link to comment Share on other sites More sharing options...
HAN! Posted October 24, 2007 Author Share Posted October 24, 2007 well thanks for these notes but i tried them and nothing worked can u take a look at my code to see if im missing somthing plz, thanks <?PHP $user='hani'; $pass='annd'; ?> <script language="JavaScript"> <!-- function validate(form) { var u = "<?php echo str_replace("\"", "\\\"", $user);?>"; var p = "<?php echo str_replace("\"", "\\\"", $pass);?>"; if (form.username.value!=u) ) { alert("Please enter the correct username"); form.username.focus(); return false; } if(form.password.value!=p) { alert("Please enter the correct password"); form.password.focus(); return false; } } //--> </script> </head> Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted October 24, 2007 Share Posted October 24, 2007 fist off do u look at the javascript errors ?, secondly u have a ) @ f (form.username.value!=u) ) { that dosent make sense. you also have <!-- comment starting may disrupt ur code if ur using ie js errors are at the bottom left small tiny icon double click it, in firefox top right, dont worry about string replace just yet use the bare variables until u can get a result Quote Link to comment Share on other sites More sharing options...
mrsquash Posted October 24, 2007 Share Posted October 24, 2007 I'm pretty sure you cannot break into <?php ?> within your <script></script> tags. You need to do something like this: <html> <head> <script language="JavaScript"> <!-- function validate() { var u = document.my_form.hide_user.value; var p = document.my_form.hide_pass.value; if (document.my_form.user_name.value != u) { alert("Please enter the correct username"); document.my_form.user_name.focus(); return; } if (document.my_form.password.value != p) { alert("Please enter the correct password"); document.my_form.password.focus(); return; } } //--> </script> </head> <body> <?PHP $user='hani'; $pass='annd'; ?> <form name="my_form" action="" method="post" enctype="multipart/form-data"> <input type="hidden" name="hide_user" value="<?php echo $user; ?>"> <input type="hidden" name="hide_pass" value="<?php echo $pass; ?>"> Username: <input type="text" name="user_name" value="" size="10"> Password: <input type="text" name="password" value="" size="10"> <a href="javascript:validate()" title="Submit" tabindex="65">Submit</a> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
fenway Posted October 24, 2007 Share Posted October 24, 2007 I'm pretty sure you cannot break into <?php ?> within your <script></script> tags. Why not? JS has no idea PHP exists, and vice-versa -- different runtime. Quote Link to comment Share on other sites More sharing options...
aleX_hill Posted October 26, 2007 Share Posted October 26, 2007 Agreed, As PHP is server-side, by the time javascript is parsed at the local browser, the PHP has done everything it needs to do. The way I do it is save the script in an external .php file, and use: <script language="Javascript" src="path_to_php_file"></script> This works for me. Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted October 31, 2007 Share Posted October 31, 2007 when i use php in javasquipt i just <script> var jsvariable = <?php $somthing; ?> </script> its just that u wont see any highlighted code so u have to know what ur doing but u wont realy need it for much or <a href="javascript:boob('<?php $myFave; ?>')"> emagine php as writing your html out for u before it gets to your browser it will fill in all the blanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.