Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My problem is the following: I have a real simple PHP file called "writeSettings2.php" that has:

<?php 
    $text=$_GET["text"];
     setcookie("MG_FileTree_Opener_SelPath", $text);
?>

And then I have my PHP page, in which I have a JavaScript function called "makeChanges", this function is called in a input type submit function and gets the combobox selected value and calls through xmlhttp the PHP function above like so:

<input type="submit" id="choice" value="Escolher" name="Esc" onClick="makeChanges()">

And the function "makeChanges" is like:

function makeChanges(  )
{
    var selObj = document.getElementById('opiniao');    
    var selIndex = selObj.selectedIndex;
    var str = selObj.options[selIndex].text;

    if (window.XMLHttpRequest)

    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();

    }
    else
    {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
   xmlhttp.open("GET","writeSettings2.php?text="+str,true);
   xmlhttp.send(null);
   history.go(0);
}

Basically what I'm doing is use the JavaScript on the client fo call PHP function to rewrite the client cookie, and then refresh the page to get PHP to rewrite the page with the new info on the cookie.

This code works fine, but only for the fist 2 times! For instance my combox has item 1 and 2. I load it and is item 1. Change to item 2. Change back to item 1. Change to item 2 again ---- FAILS never changes again, it always remains on 1.

Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
873 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your browser is caching the ajax response. You need to add a random value to the ajax request, or put the no-cache header in the called page. You can change the javascript like so to fix it:

var str = selObj.options[selIndex].text+'&random='+(new Date()).getTime();

And/Or use the following php header:

header("Cache-control: no-cache");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...