Jump to content

Noob question - pass Javascript variable into hidden input field


Recommended Posts

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!

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'];

 

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).

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);

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.