Forum Moderators: open

Message Too Old, No Replies

set cookie without reloading page

ajax newbie!

         

cantona

3:20 pm on Jul 14, 2008 (gmt 0)

10+ Year Member



Hello!

I'm new to AJAX to bare with me....

I have a select menu. OnChange I'd like to set a cookie with the value of the select menu.

I'm setting the cookie in a PHP file (if you can set cookies in javascript let me know :)

Here's what I got...


HTML

<select name="search_mode" id="search_mode" onchange='aj_file("/ajax/color.php", this.value)'>
<option value="rd">red</option>
<option value="bl">blue</option>
</select>


JAVASCRIPT/AJAX

(At top I'm testing ActiveXObject and setting up xmlhttp... same stuff as everyone else)

function aj_file(serverPage, value) {
xmlhttp.open("GET", serverPage, true);
xmlhttp.send(value);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
return xmlhttp.responseText;
}
}

PHP
$value = $_GET['value'];
setcookie('topsearch', $value, time() + 900000000, "/");


I just want to pass the value to the php file so that I can save it as a cookie. There doesn't have to be a response or confirmation.

Please help! :)

Fotiman

4:51 pm on Jul 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, you can set cookies in JavaScript. Here's a tutorial:
[w3schools.com...]

cantona

7:32 am on Jul 15, 2008 (gmt 0)

10+ Year Member



Thanks Fotiman, thats good to know. I could probably just use that in my script and not bother with the AJAX stuff for now.
Still, it would be good to know where I'm going wrong here so I can use it in the future.
Any AJAX experts out there?