melting_dog Posted November 7, 2011 Share Posted November 7, 2011 Hi all, Im sure this is very simple but I couldnt find anything on the web that could help me. All I want to do is pass a JavaSript variable from one page to another via the hidden form field. Something like this: <input type="hidden" name="date" id="date" value="<script>document.write(curdate);</script>"/> Can anyone advise me on how best to do this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/250581-noob-question-pass-javascript-variable-into-hidden-input-field/ Share on other sites More sharing options...
haku Posted November 7, 2011 Share Posted November 7, 2011 document.getElementById("date").value = variable Quote Link to comment https://forums.phpfreaks.com/topic/250581-noob-question-pass-javascript-variable-into-hidden-input-field/#findComment-1285779 Share on other sites More sharing options...
melting_dog Posted November 7, 2011 Author Share Posted November 7, 2011 Thans Haku, Sorry for my ignorance but is that what I would place on the next page? Is my syntax above ok? I was hoping to pass that variable as a string so I could use it in PHP - eg: $curdate=$_POST['curdate']; Quote Link to comment https://forums.phpfreaks.com/topic/250581-noob-question-pass-javascript-variable-into-hidden-input-field/#findComment-1286068 Share on other sites More sharing options...
haku Posted November 8, 2011 Share Posted November 8, 2011 Your syntax isn't wrong (I'd do it differently myself, but what you have should work), but whether or not you are able to access that value on the next page is dependent on how the page is changed. Hidden inputs are form elements, and form elements are only available to PHP if they are part of a form that is submitted. So if you have the user clicking a submit button, then the value will be available to your PHP script on the next page. But if you have the page changing by a link or by javascript, then the value won't be available to PHP on the next page (though theoretically you could use your javascript to submit a form that would make it available). If you want to pass it to the next page, you can use cookies, or you can set a GET variable in the URL (?something=something else). Quote Link to comment https://forums.phpfreaks.com/topic/250581-noob-question-pass-javascript-variable-into-hidden-input-field/#findComment-1286124 Share on other sites More sharing options...
melting_dog Posted November 9, 2011 Author Share Posted November 9, 2011 Hi Haku, Yes the hidden field is part of a form with a post method. All other fields in the form (text fields) are able to be received by the php on the other page. Its just this hidden field with the javascript that is not working. Here it is in more detail - its just meant to be passing the date and time the form was completed so the DB is updated: <!--GET CURRENT DATE--> <script type="text/javascript"> var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() var curdate = (day + "/" + month + "/" + year) </script> <form id="submitphotodetails" method="post" action="submitphotodetailprocess.php" enctype="multipart/form-data"> <input type="hidden" name="date" id="date" value="<script>document.write(curdate);</script>"/> ---OTHER FORM FIELDS AND SUBMIT BUTTON--- And on the next page: $date=$_POST['date']; $date = stripslashes($date); $date = mysql_real_escape_string($date); Quote Link to comment https://forums.phpfreaks.com/topic/250581-noob-question-pass-javascript-variable-into-hidden-input-field/#findComment-1286462 Share on other sites More sharing options...
haku Posted November 9, 2011 Share Posted November 9, 2011 Try this: <!--GET CURRENT DATE--> <script type="text/javascript"> function getDate() { var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() var curdate = (day + "/" + month + "/" + year) return curdate; } <input type="hidden" name="date" id="date" onload="this.value = getDate();"/> I'm not 100% sure this will work, as I said, I would do this differently. Quote Link to comment https://forums.phpfreaks.com/topic/250581-noob-question-pass-javascript-variable-into-hidden-input-field/#findComment-1286494 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.