Jump to content

[SOLVED] Pass javascript variable to php variable (same page- seamlessly)


DamienRoche

Recommended Posts

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.

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

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

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?

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.

 

 

Archived

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

×
×
  • 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.