View Full Version : Passing Javascript values to PHP variables?
Glass Casket
07-31-2006, 04:34 PM
I wasen't too sure if this belonged in the Javascript or PHP section. But I figured it belonged in here because I'll be dealing with session variables.
So I'm trying to make it so everytime the user clicks on a link, it registers a session variable from a Javascript value. So if they click back, it shows up the same results they had before they clicked elsewhere.
I Googled around and found a few examples, but nothing seemed of what I was looking for. I have tried a combination of things, but nothing as of yet. To be more precise, I have a Javascript variables called `view_when` and want to register it to $_SESSION["position"].
Thanks.
Edit: I found another way of doing it.
Beagle
07-31-2006, 05:23 PM
You have to understand the way things work.
The only way a web server gets and sends information is through requests and responses, respectively. The web server has no concept of javascript, or for that matter, php. You tell your server that when you have to serve a file with the .php extension, send the request to this application over here (php processor) and respond with the return value from this application.
So knowing that, how does PHP know about variables from forms and what not? PHP, built into the initial processing of a script, reads the request that the web server got from the browser, and parses it out to find variables. It's like there's a secret code that PHP and the browser use that your webserver doesn't know about, it just passes the message along.
Knowing that, how would you get a JavaScript variable from the browser to PHP? You'd have to send it in your request. The two methods of doing so that are used are GET and POST. GET encoding is the familiar url?varName=val&varName2=val2, POST is encoded and you don't see it.
So, if you have a user clicking something, and it generates a request (either an <a href> or an XMLHTTPRequest) then you need to take your client side javascript variable and get into the request, either with GET or POST encoding. The simplest is clearly GET since you can just conatenate your value to your URL, but depending on how complex your system it, you may not be able to easily do that.
I hope that clear ups what you're trying to do and that it sheds light on your solution:
<a href="clickHere.php?position=" onclick="this.href+=view_when">Click Here</a>
<?php
session_start();
$_SESSION['position'] = $_GET['position'];
?>
Glass Casket
07-31-2006, 06:56 PM
Thanks for that throughroug explanation. It's pretty damn clear now! ;)
Glass Casket
07-31-2006, 09:17 PM
Actually, having played with it for a bit more, I came to the conclusion that it dosen't work at all. :(
switch(time_period) {
case "today": view_when = "today"; <?php $_SESSION["selected_day"] = "today"; ?> alert("<?php echo $_SESSION["selected_day"]; ?>"); break;
case "tomorrow": view_when = "tomorrow"; <?php $_SESSION["selected_day"] = "tomorrow"; ?> break;
case "yesterday": view_when = "yesterday"; <?php $_SESSION["selected_day"] = "yesterday"; ?> break;
case "this_week": view_when = "this_week"; <?php $_SESSION["selected_day"] = "this_week"; ?> break;
case "next_week": view_when = "next_week"; <?php $_SESSION["selected_day"] = "next_week"; ?> break;
case "last_week": view_when = "last_week"; <?php $_SESSION["selected_day"] = "last_week"; ?> break;
case "next_two_weeks": view_when = "next_two_weeks"; <?php $_SESSION["selected_day"] = "next_two_weeks"; ?> break;
}
The session variables set itself fine. But for some odd reason, it's always set to `next_two_weeks`, and if I take out that case section, it simply end up registering the case on top of it. What did I miss?
Thanks.
ralph l mayo
07-31-2006, 09:38 PM
You're missing the whole point of the long post above. The logic you describe will not work.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.