By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

passing php date variable to javascript function

P: 59
Hi all,

How to pass the php date variable to javascript function. Here is the code I have tried out, but this is not recovering the date correctly in the javascript function

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <script type="text/javascript">
  3.  
  4. function add($date1){
  5.  
  6. alert($date1)
  7. }
  8. </script>
  9. <body>
  10. <?php
  11. $date1 = date("y-m-d");
  12. <input type="submit" value="GO" onclick="add('.date1.')">
  13. ?>
  14. </body>
  15. </html>
When I alert the date1 variable in javascript, it will give some junk value instead of date. How to get the date value into javascript variable

With regards
Feb 23 '08 #1
Share this Question
Share on Google+
5 Replies


P: 13
please ignore this see the below one
Feb 23 '08 #2

P: 13
Hi all,

How to pass the php date variable to javascript function. Here is the code I have tried out, but this is not recovering the date correctly in the javascript function

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <script type="text/javascript">
  3.  
  4. function add($date1){
  5.  
  6. alert($date1)
  7. }
  8. </script>
  9. <body>
  10. <?php
  11. $date1 = date("y-m-d");
  12. <input type="submit" value="GO" onclick="add('.date1.')">
  13. ?>
  14. </body>
  15. </html>
When I alert the date1 variable in javascript, it will give some junk value instead of date. How to get the date value into javascript variable

With regards

Its simple instead of writing
Expand|Select|Wrap|Line Numbers
  1.  
  2. function add($date1){
  3.  
  4. alert($date1)
  5. }
  6.  
just write it like this
Expand|Select|Wrap|Line Numbers
  1.  
  2. function add(date1)
  3. {
  4. alert(date1);
  5. }
  6.  
  7. <input type="submit" value="GO" onclick="add('<? echo $date1; ?>')">
  8.  
Do above changes and it will definately work. BEST OF LUCK
Feb 23 '08 #3

Markus
Expert 5K+
P: 6,050
Remember to keep your php seperate from your javascript!

JS will have a fit if you try and include a php $ variable ;)
Feb 23 '08 #4

P: 59
thank you all for your response
Feb 25 '08 #5

P: 4
Data passing from Javascript, PHP, HTML

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 #6

Post your reply

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