424,279 Members | 1,907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,279 IT Pros & Developers. It's quick & easy.

Passing values from PHP to JavaScript

darksteel21
P: 36
can anyone tech me how yo pass values from php script to javscript??
Aug 5 '08 #1
Share this Question
Share on Google+
8 Replies


Gulzor
P: 27
A simple output does the job :

[php]
<script type="text/javascript">
function hello(name) {
alert("hello "+name);
}

<?php
while ( $rec = mysql_fetch_assoc($query) ) {
echo 'hello("',$rec['firstname'],'");', "\n";
}
?>

// or

hello("<?php echo $var; ?>");

</script>
[/php]
Aug 5 '08 #2

dlite922
Expert 100+
P: 1,584
we usually put the echo or print line in the value of a "hidden" text box.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <input type="hidden" name="someVar" id="someVar" value="<?php echo $myVar; ?>" />
  3.  
  4.  
and then you can reach it via javascript once the page has been written (remember javascript = runs on client computer, PHP executes on the server before its delivered to the client computer)

So the browser sees the page as

[HTML]

<input type="hidden" name="someVar" id="someVar" value="YOUR_MY_VAR_VALUE_HERE" />

[/HTML]

and now JavaScript can access it this way:

Expand|Select|Wrap|Line Numbers
  1. alert("Value is " + document.getElementById("someVar").value);
  2.  
  3.  

If you're printing this value elsewhere on the page, put inside a tag and give that tag an id for example:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <div id="container">
  3.    <span id="someVal"><?php echo $myVal; ?></span>
  4. </div>
  5.  
  6.  
and you can access that with the attribute "innerHTML" such as

Expand|Select|Wrap|Line Numbers
  1.  
  2. alert("Value is " + document.getElementById("someVar").innerHTML);
  3.  
  4.  
if you STILL don't get it, Google: "miracle".


Dan
Aug 5 '08 #3

Atli
Expert 5K+
P: 5,058
we usually put the echo or print line in the value of a "hidden" text box.
...
Just a thought.
Do you do this just to pass the values into JavaScript?

If so, you could simply add it to the header as a global variable.
Like:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.   <title>Test stuff</title>
  4.   <script type="text/javascript">
  5.     // Create a var in the global scope.
  6.     // Can be accessed by any Javascript that follows, anywhere in the page.
  7.     var phpValue = "<?php echo $someValue; ?>";
  8.   </script>
  9. </head>
  10.  
  11. <!-- This should print the value when the page loads -->
  12. <body onload="javascript: alert(phpValue);">
  13.   <h1>Nothing to see here... move along</h1>
  14. </body>
  15. </html>
  16.  
Doesn't really make sense to me to add actual HTML markup for each variable, when you can simply add it to a Javascript block.
Aug 6 '08 #4

samikhan83
P: 33
i agree with atli ....

if u want to pass php value to javascript as parameter then u can do it this way...

say u have a function in javascript by name passPhpValue(parm1);
Expand|Select|Wrap|Line Numbers
  1. passPhpValue(parm1)
  2. {
  3. alert("This is PHP value:"+parm1);
  4. }
  5.  
  6. javascript:onload="passPhpValue(<?=$phpvalue?>)";
  7.  
Aug 6 '08 #5

Gulzor
P: 27
i agree with atli ....

if u want to pass php value to javascript as parameter then u can do it this way...

say u have a function in javascript by name passPhpValue(parm1);

passPhpValue(parm1)
{
alert("This is PHP value:"+parm1);
}

javascript:onload="passPhpValue(<?=$phpvalue?>)";
You should not use this <?=$var?> (on its way to deprecation), but this <?php echo $var; ?> instead.
Aug 6 '08 #6

Atli
Expert 5K+
P: 5,058
You should not use this <?=$var?> (on its way to deprecation), but this <?php echo $var; ?> instead.
A good point, but the short-tags have not been made deprecated just yet.
They will still exist in PHP6, but like in PHP5, they will not be enabled by default.
To use them you must reconfigure PHP.

So, like you say, using the short-tags should be avoided at all costs. By using the standard tags <?php ... ?>, you ensure that your code will always work as expected, no matter how the server is configured.
Aug 7 '08 #7

samikhan83
P: 33
A good point, but the short-tags have not been made deprecated just yet.
They will still exist in PHP6, but like in PHP5, they will not be enabled by default.
To use them you must reconfigure PHP.

So, like you say, using the short-tags should be avoided at all costs. By using the standard tags <?php ... ?>, you ensure that your code will always work as expected, no matter how the server is configured.


thanx Atli

for the information
from now on i will use <?php echo $id?> instead of <?=$id?>
Aug 7 '08 #8

P: 4
Expand|Select|Wrap|Line Numbers
  1. <?php  $xphp= "My PHP WORLD"; ?>   <!-- Define a PHP variable -->
  2. <script> var xjs='My Java Script world'; </script> <!-- Define a JAVASCRIPT variable -->
  3. <input type='hidden' id='myhtml' value='My HTML world!' > <!-- Define a HTML variable -->
  4.  
  5. <BR>sending PHP variable value into JAVASCRIPT <BR>
  6. <script>
  7. var xphp='<?php echo $xphp; ?>';
  8. document.write(xphp);    
  9. </script>
  10.  
  11. <BR>getting PHP variable value into HTML <BR>
  12. <?php echo $xphp; ?>
  13.  
  14. <BR><BR>getting JAVASCRIPT  variable value into PHP <BR>
  15. <?php
  16. $xjs = "<script>document.write(xjs);</script>";
  17. echo $xjs;
  18. ?>
  19.  
  20. <BR>getting JAVASCRIPT  variable value into HTML <BR>
  21. <script>document.write(xjs);</script>
  22.  
  23. <BR><BR>getting HTML variable value into JAVASCRIPT  <BR> 
  24. <script>
  25. var xhtml=document.getElementById('myhtml').value;
  26. document.write(xhtml);    
  27. </script>
  28.  
  29. <BR>getting HTML variable value into PHP <BR> 
  30. <?php
  31. $xhtml = "<script>document.write(document.getElementById('myhtml').value);</script>";
  32. echo $xhtml;
  33. ?>
Mar 27 '17 #9

Post your reply

Sign in to post your reply or Sign up for a free account.