homepage Welcome to WebmasterWorld Guest from 54.196.18.51
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
javascript variable posted to PHP variable
hermes




msg:1316440
 9:13 pm on Dec 26, 2004 (gmt 0)

Merry christmas everybody.

I wish to send a javascript variable, the website visitors time zone, to a PHP page, by way of a HIDDEN form submission (using the POST method). BUT I do NOT want to send this javascript variable to a different page - but to the SAME page. Then to display it on this same page.

I have been banging my head against the wall on this for quite while now. Would be really grateful if someone could help me out. Below is my best stab at the coding - it is not working. I realise that the problem is probably related to my not refreshing the page in the right place. I know this
much - but I dont know where to go from there.

<html>
<body>

<SCRIPT LANGUAGE='JavaScript'>
var now = new Date();
var time = now.getTimezoneOffset();
</SCRIPT>

<?php if (empty($_POST['tz'])){?>
<form name='detect' method='post'
action=<?php print $PHP_SELF;?>>
<input type='hidden' name='tz' value='time'>
</form>
<body onLoad="myform.submit()">
<?php }?>
<?php echo $_POST['tz']
?>
</body>
</html>

Once again, merry christmas.

P.S I really want to use the POST method as opposed to the GET method for this.

 

hermes




msg:1316441
 2:52 pm on Dec 27, 2004 (gmt 0)

Been trying for a few more hours to sort this out, This seems to be a real wall for me. I think i am so close - just need someone to give me a nudge. Would really appreciate anyone taking a look at this. Many thanks, Happy boxing day.

Bonusbana




msg:1316442
 3:05 pm on Dec 27, 2004 (gmt 0)

Im not sure about this, but if the form is printed, you will end up with 2 <body> tags wich might confuse the browser. I also think that the form value attribute "time" is simply the text "time" and not the javascript variable that you want.

Why dont you just detect timezone via PHP directly, or re-load the browser with the time-zone variables in the url?

hermes




msg:1316443
 7:40 pm on Dec 27, 2004 (gmt 0)

Spotted one mistake. The line:

<?php echo $_POST['tz']

has a ; missing. Should be

<?php echo $_POST['tz'];

Although this does not get the code working unfortunately. Help!

jatar_k




msg:1316444
 8:12 pm on Dec 27, 2004 (gmt 0)

well there are many problems with that code

2 body tags is one
the fact that the page will always produce a javscript error on the second post because the form won't exist
your onLoad statement wasn't working

I reworked it to this

<html>
<head><title></title>
</head>
<body onLoad="document.forms[0].submit()">
<? if (empty($_POST['tz'])) {?>
<form name="detect" method="post" action=<?= $_SERVER["PHP_SELF"];?>>
<SCRIPT LANGUAGE="JavaScript">
var now = new Date();
var time = now.getTimezoneOffset();
document.write('<input type="hidden" name="tz" value="' + time + '">');
</SCRIPT>
</form>
<? } else {
echo $_POST['tz'];
}
?>
</body>
</html>

I am not sure if it is working properly but it does return a number now, it should get you going at least

hermes




msg:1316445
 10:07 pm on Dec 27, 2004 (gmt 0)

I'm really sorry to be a pain mate but I cannot get it working for me.

It is supposed to return a number - the timezone (how many hours away from GMT). For me it would be 0 as I am in the GMT = 0 timezone.

So, you manage to get a number returned? Can anyone else get a number returned? Or is it just me with a no show?

What I get - just a blank page. The curser looks normal. But when I place it over the browser toolbar - it is going crazy. Flickering (is that a word?) like crazy. Indicating that something at least is going on behind the scenes.

I am using something called easyphp. This is a version of apache, mysql, php etc for a desktop computer. So that one can test PHP scripts on a home computer.

If the code is working fine for you guys out there, I will start to look into the configuration of the system that I am using to run the PHP.

Once again mate. Sorry to be such a pain.

hermes




msg:1316446
 10:15 pm on Dec 27, 2004 (gmt 0)

I've got some very similar code here - aiming to do the same thing - that I also cannot get to work. Does it work for anyone else?

<html>
<head>
<?php
if(!isset($_POST['tz'])) {
?>
<script type="text/javascript">
window.onload = function() {
//document.detect.submit();
}
</script>
<?php
}
?>
</head>
<body>
<?php
if(!isset($_POST['tz'])) {
?>
<form name='detect' method='post' action='<?php echo $_SERVER['PHP_SELF'];?>' onsubmit='this.tz.value = new Date().getTimezoneOffset(); return true;'>
<input type='hidden' name='tz' value=''>
</form>
<?php
}
else echo $_POST['tz'];
?>
</body>
</html>

copongcopong




msg:1316447
 6:39 am on Dec 28, 2004 (gmt 0)

have you tried not to use the body onload but instead add a script tag on the last part of your page (before </body>) and add the form submission script (document.detect.submit();)?

This will make it sure that the form info is already loaded before your script try to submit it.

hermes




msg:1316448
 10:38 pm on Dec 28, 2004 (gmt 0)

Thanks ever so much guys. I have finally crossed the line. I cant thank you enough. I was really stuck. Thanks also to the guys that stickied me. This board really is fantastic.
All the best for the new year.
herm

mincklerstraat




msg:1316449
 11:00 am on Dec 29, 2004 (gmt 0)

fwiw here's a solution I sent in answer to a sticky from Hermes, caveat scriptor I'm not a js expert:


<head>
</head>
<html>
<body>
<?php if (empty($_POST['tz'])){
?>
<form name='detect' method='post'
action=<?php print $PHP_SELF;?>>
<input type='hidden' name='tz' value='time' />
</form>
<script language="JavaScript"><!--
var now = new Date();
var time = now.getTimezoneOffset();
window.document.detect.tz.value = time;
window.document.forms['detect'].submit();
-->
</script>
<?php
}
echo $_POST['tz'];
?>
</body>
</html>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved