Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing javascript variables through hidden input

Status
Not open for further replies.

breaststroke

Programmer
Apr 10, 2011
39
ES
Hello, how are you!


I am working on php and trying to add some javascript code because I would like to get user's local timezone (GMT/UTC) and keep it in my database.

To do that I create a variable called GMT and try to pass it into another page through a form.

I have read something about it and I have come up with the following way to do it. But it is not working and I can not figure out why.
Well, it worked at the beginning on Google Chrome, but not on Firefox. Trying to fix it..now it doesn't work on any browser (I would swear now it's as it was at first..).


I would appreciate any help.

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


document.getElementById("utc").value=GMT;
</script>
<?php
..
..
HERE;

When trying to get its value on the page the form is addressed to I get nothing.
I try it doing this:

Code:
...
$utc=$_POST['utc'];
echo $utc;

..
..

p.s. sorry to insist, but it worked fine at the beginning. I just tried to change its position or something. Now even though I am pretty sure it is placed on the same point, I can't make it work.

I would be more than happy to get any idea about it.

Regards!!
 
Hi

The fragments you posted are almost completely useless in debugging your code. Even more you neither posted explanations like how that form is submitted.

If you want help, post all the involved code.

Feherke.
 
Hello again,

thank you for replying feherke.

as I said that I was working on php I thought it was enough showing the only javascript code I have added on the file with the form. I add more code now.
I tried to edit the second code fragment after submitting it, but I failed.

The form (on the first page)which includes javascript code is something like this:

Code:
<?php
..
..
<form method="post" action="anotherpage.php" >
..
<input ..
<input..
<input type="hidden" name="utc" id="utc" />
..
?>
<script type="text/javascrip">
var d = new Date();
var GMT = -d.getTimezoneOffset()/60;
document.getElementById("utc").value=GMT;
</script>
<?php
..


<button type="submit">Submit</button>
</form>

?>
..

Then I take every variable on anotherpage.php :

Code:
..

<?php
..
$utc=$_POST['utc'];
..
$otravariable=$_POST['otravariable'];
...
?>
..


Thank you
 
Well this would cause of a problem if that's how your code really looks like.

Code:
<script type="text/javascrip[red]_[/red]">

The missing T prevents anything inside form getting run as Javascript.

If that's not really present as an error, then we'll need to actual code you are working with. Copy and Paste it as is, otherwise we are just guessing.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hello,

first of all, thank you for your replies, and for your correction, vacunita (it was a typo).
And sorry, because although my code is pretty much like I have written it here, it is not exactly the same.
As I had worked long ago on this file I had forgotten that there was a conditional loop in it that I had to remove since it doesn't make any sense being there.
The input I was including this javascript variable in was inside
that loop. I wasn't noticing it!.
I was trying to simplify my code since the file it is within is about 3000 lines long.

Now it is working fine, both in Chrome and Firefox (and I suposse also in others).

Byee,

best regards!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top