Jump to content

to send a variable through a hidden input


breaststroke

Recommended Posts

Hello !I think what I am trying to do is not very difficult , but I can't come up with the way to do it. I don't know very much about javascript.I hope someone can help me.I want to get the user's UTC/GMT timezone and send it to another page.I make the following:

<?php...print<<<HERE..<input type="hidden" id="utc" name="utc"><script>var d = new Date();var GMT = -d.getTimezoneOffset()/60;document.getElementById("utc").value=GMT;</script>....HERE;

I know the script to get the UTC/GMT is correct (in itself) because I have tested it alone, out of any form.To my surprise I have learnt I have to include the javascript script into the "printing" area so that not to get an error.And, in the following page (the one the form is addressed to), I try to get the "utc" variable by $_REQUEST['utc'], like I do with the other variables from the form, but I get nothing.I guess I am doing several things wrong. I have also tried writing "document.write(GMT)" instead of just "GMT" when I give the value to documentElementById..I would really appreciate any help.Regards

Link to comment
Share on other sites

Is that input tag inside the Javascript code? You shouldn't have any HTML in there, that's only for Javascript. Your HTML needs to go before the script tag.
hi,thank you for your reply justsomeguy. That was a mistake of mine. I have corrected it right now.I had tested it the way it is now.Sorry again,
Link to comment
Share on other sites

hi,thank you for your reply justsomeguy. That was a mistake of mine. I have corrected it right now.I had tested it the way it is now.Sorry again,
I did it, hehe.My code was wrong because javascript code must be outside php (maybe you were warming about it)So this is what I have made:
print<<<HERE<?php.... <input type="hidden" name="utc" id="utc" value="">HERE;?><script type="text/javascript">var d = new Date();var GMT = -d.getTimezoneOffset();document.getElementById("utc").value=GMT;</script><?php..<input submit....}

Also, value="" seems to be optional. At least in my case it worked without it too.Byeeeee! :)

Link to comment
Share on other sites

hi,well, I don't understand this at all.It was working fine on Google Chrome, but when I tried on Firefox it didn't work.After trying to make some changes, (just moving the code to other places), and restarting my computer, now it doesn't even work on G Chrome. And I have set everything up as it was before, so the same as it is on my previous post.Any idea, what can be wrong?Thank you in advance, regardsp.s: when I say that it isn't working I mean that I can't get the value back; it gives nothing, as it it were empty.

Link to comment
Share on other sites

hello,I am coming back just to say it seems I have solved it.The input I was adding the javascript variable to was actually in a conditional loop that didn't make any sense.I hadn't notice it since I had writen it long ago.Now that I have removed it the script is running fine and I get the variable value on the other page on both browsers.See you!,regards

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...