Connecting Tech Pros Worldwide Forums | Help | Site Map

passing php date variable to javascript function

Member
 
Join Date: Jan 2008
Posts: 59
#1: Feb 23 '08
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

Newbie
 
Join Date: Jan 2008
Posts: 13
#2: Feb 23 '08

re: passing php date variable to javascript function


please ignore this see the below one
Newbie
 
Join Date: Jan 2008
Posts: 13
#3: Feb 23 '08

re: passing php date variable to javascript function


Quote:

Originally Posted by gubbachchi

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
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#4: Feb 23 '08

re: passing php date variable to javascript function


Remember to keep your php seperate from your javascript!

JS will have a fit if you try and include a php $ variable ;)
Member
 
Join Date: Jan 2008
Posts: 59
#5: Feb 25 '08

re: passing php date variable to javascript function


thank you all for your response
Reply