DamienRoche Posted September 19, 2008 Share Posted September 19, 2008 Can I pass a javascript variable to a php variable on the same page? I know I'll probably have to use a nifty redirect..but how do I go about it. So I have: javascript! var = "my new var"; PHP! phpvar = "var"; ///obviously this won't work, just to illustrate what I'm looking for. It needs tobe very seamless without using GET from the browser address bar. Any input is greatly appreciated. Thanks. Link to comment https://forums.phpfreaks.com/topic/124934-solved-pass-javascript-variable-to-php-variable-same-page-seamlessly/ Share on other sites More sharing options...
aschk Posted September 19, 2008 Share Posted September 19, 2008 There are 2 methods you have highlighted here. 1) POST a form to a php script (page). 2) GET a php script (url) with a param (e.g. index.php?var=my%20new%20var ) You need to understand that javascript is client side (i.e. in the browser) and that PHP is server side. You need to send a request to the server to set this variable. AJAX is an option, but i think you'd better consider just calling a page first Link to comment https://forums.phpfreaks.com/topic/124934-solved-pass-javascript-variable-to-php-variable-same-page-seamlessly/#findComment-645537 Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 AJAX is an option, but i think you'd better consider just calling a page first AJAX is 'calling a page.' There's no way for JS to interact with PHP without the client sending the server some data ( in either a GET or POST ). This can be done very 'seamlessly' with javascript, but it would rely on either using invisible iframes or XMLHttpRequest(); Link to comment https://forums.phpfreaks.com/topic/124934-solved-pass-javascript-variable-to-php-variable-same-page-seamlessly/#findComment-645548 Share on other sites More sharing options...
DamienRoche Posted September 19, 2008 Author Share Posted September 19, 2008 THIS IS DRIVING ME NUTS. All I want to do is get the current date using javascript, but if javascript is disabled, get the date of the server using php...all seamlessly on the same page. JAVASCRIPT: function checkjavatime(){ var phptime ="<?php echo "$todayserver"; ?>"; var javatime ="<?php echo "$todayjava"; ?>"; if(phptime=="" && javatime==""){document.timeform.submit();}else{return false;} } HTML: <body onload="checkjavatime();"> <form name="timeform" method="post" action="index.php"> <input type="hidden" name="datecheck" value="19" /> </form> </body> AND THE PHP: $todayjava = $_POST['datecheck']; if($todayjava!=""){ echo "today from form:$todayjava"; } if($todayjava==""){ $todayserver = date("j"); echo "today from server:$todayserver"; } Obviously, because the php is parsed before the javascript the php if statement does not work, because how can the javascript variable be there? So all this does is check if first check if the form has submitted the date, when it hasn't it returns the variable and thent he javascript checks and sees that that variable is there so it doesn't submit the form. Any pointers? Link to comment https://forums.phpfreaks.com/topic/124934-solved-pass-javascript-variable-to-php-variable-same-page-seamlessly/#findComment-645559 Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 Not really. PHP doesn't know if JS is enable or disabled, and after that it's too late. You could have a meta redirect enclosed in <noscript> tags that points to itself with something like self.php?js=off... then PHP would know javascript was turned off. This would require a page refresh though. Link to comment https://forums.phpfreaks.com/topic/124934-solved-pass-javascript-variable-to-php-variable-same-page-seamlessly/#findComment-645562 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.