473,327 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

passing php date variable to javascript function

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
5 10141
nehas
13
please ignore this see the below one
Feb 23 '08 #2
nehas
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
6,050 Expert 4TB
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
thank you all for your response
Feb 25 '08 #5
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

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

Similar topics

1
by: Patrice | last post by:
Hi, I'm trying to use a javascript function passing a vb variable. I don't understand why it doesn't work. Can someone help me? Thanks in advance. Here is my javascript function used to...
1
by: Kevin Lyons | last post by:
Hello, I am trying to get all of my form elements passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html to the following URL:...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
9
by: Max | last post by:
I'm new with Javascript and can't seem to figure out what I'm doing wrong here as I'm not able to pass a simple variable to a function. In the head of doc I have: <script...
3
by: IntraRELY | last post by:
I have the following function, Notice how I am passing the dateInterval as a string. What is the correct way to pass "DateInterval.Year" as a variable to a function? TIA, Steve Wofford...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
2
by: Geoff Cox | last post by:
Hello, The code below is aimed at passing the date in the yyyyMMdd format from the javascript calendar in an html file to the php in a another file which then searches a MySQL database. For...
1
by: satish2112 | last post by:
Hi, I have a text-area which contains values from mysql database and 2 buttons, Edit and Update. When I click on the Edit button, I can edit the text-area (initially non-editable). After this,...
3
by: gubbachchi | last post by:
Hi, How to get the value passed using javascript in the next page. The code I have used is this. cal.php <html> <head> </head>
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.