Forum Moderators: coopster

Message Too Old, No Replies

Passing a Javascript variable value to php

Passing a Javascript variable value to php

         

bourbon_33

9:31 am on Nov 10, 2005 (gmt 0)

10+ Year Member



$total = "<script language=javascript>document.write(subtotal);</script>";
echo $total;

When I run the program I get a display on the screen of "59.90" which is correct.

But when I insert the $total variable into my email page the variable $total writes "<script language=javascript>document.write(subtotal);</script>" to my email instead of the value.

Can anybody help.

Thanks

Hester

10:48 am on Nov 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Think about what is happening. The server is running PHP. It generates the page, including your JavaScript. Then, the client, which is the browser, acts on the page. It finds the JavaScript and displays the total figure. But the page is no longer on the server, but in your computer's cache.

You need a way to get the variable back onto the server, so PHP can use it.

The best way I have found for a project I did was to use cookies. Since both JavaScript and PHP can read these, create one using JavaScript to store the total figure. Then on a new page, put in some PHP to read the cookie.

A reverse method to do this I have recently devised, is to have a variable in PHP and put it in a hidden form input. Then you can use JavaScript to read the value of the input, if it has an ID.

So it's possible to pass variables from JavaScript to PHP and back! Messy, but sometimes it has to be done.

infpack

12:15 pm on Nov 11, 2005 (gmt 0)

10+ Year Member



You need to set up hidden form variables.
Then set the form variables using javascript and submit them along with the other form data.

<snip>.

<snip>

[edited by: engine at 2:06 pm (utc) on Nov. 11, 2005]
[edit reason] No Sigs/self promo, thanks, See TOS [/edit]

Anyango

1:03 pm on Nov 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



a bit Off Thread, but not Off Charter, i feel the last post contains personal promotion url and i reckon this post will be edited by the administrators as soon as they notice it.

sunveria

11:13 am on Nov 22, 2005 (gmt 0)

10+ Year Member



use cookie to passing javascript variable to php variable.

see this example. (I got the user screen resolution from javascript and then passing it to php)

File a.html
<HTML>
<TITLE>Getting screen resolution........ Please wait....................</TITLE>
<!--
no personal urls please
-->
<HEAD>

<script language="javascript">
<!--
writeCookie();

function writeCookie()
{
var today = new Date();
var the_date = new Date("December 31, 2099");
var the_cookie_date = the_date.toGMTString();
var the_cookie = "ur_width=" + screen.width ;
var the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie=the_cookie
var the_cookie = "ur_height=" +screen.height;
var the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie=the_cookie

location = 'b.php';
}
//-->
</script>

</HEAD>
<BODY>

Detecting your screen resolution .........<br>Please wait.....................................

</BODY>
</HTML>

File b.php
<?php
if(isset($_COOKIE["ur_width"]) and isset($_COOKIE["ur_height"])){
$ur_width = $HTTP_COOKIE_VARS["ur_width"];
$ur_height = $HTTP_COOKIE_VARS["ur_height"];
} else {
header("Location: a.html");
}
//delete the cookie
setcookie("ur_width",'',time()-3600);
setcookie("ur_height",'',time()-3600);
?>

[edited by: coopster at 4:46 pm (utc) on Nov. 22, 2005]
[edit reason] removed ulr per TOS [webmasterworld.com] [/edit]