How to assign a PHP variable value to JavaScript variable -
in code posting have 1 problem. want php variable stored in javascript variable shows error. code below.
<?php $name="anurag singh"; echo ' <html> <head> <script type="text/javascript" src="jquery-2.0.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ var name1=.$name.";" $.post("main_page.php",{input1: name1}, function(msg){ alert("hi "+msg); }); }); </script> </head> <body> <h1>this demo!</h1> <h2>in echo can add more 1 statements</h2> </body> </html> '; ?>
now when assigning $name
variable name1
syntax error. please let me know changes have make. value of php variable $name
stored in javascript variable name1
.
in javascript echo version: var name1= "'.$name.'";
<?php $name = "anurag singh"; echo ' <html> <head> <script type="text/javascript" src="jquery-2.0.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ var name1= "'.$name.'"; $.post("main_page.php",{input1: name1}, function(msg){ alert("hi "+msg); }); }); </script> </head> <body> <h1>this demo!</h1> <h2>in echo can add more 1 statements</h2> </body> </html> '; ?>
and can use var name1= "<?php echo $name; ?>";
seperated html , php
<?php $name="anurag singh"; ?> <html> <head> <script type="text/javascript" src="jquery-2.0.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ var name1= "<?php echo $name; ?>"; $.post("main_page.php",{input1: name1}, function(msg){ alert("hi "+msg); }); }); </script> </head> <body> <h1>this demo!</h1> <h2>in echo can add more 1 statements</h2> </body> </html>
Comments
Post a Comment