简体   繁体   中英

How to refresh a div tag without losing the posted data using AJAX and PHP

Now I got two pages HTML page that contains Ajax refresh div tag code and posted data from another page using $_POST and the second page is PHP page that Select data from a mysql table, So let's see the code that I use to reload a div tag using AJAX:

function Ajax(){
var xmlHttp;
    try{    
        xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
    }
    catch (e){
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
        }
        catch (e){
            try{
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e){
                alert("No AJAX!?");
                return false;
            }
        }
    }

xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState==4){
        document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText;
        setTimeout('Ajax()',2000);
    }
}
xmlHttp.open("GET","data.php",true);
xmlHttp.send(null);
}

window.onload=function(){
    setTimeout('Ajax()',2000);
}

That code reload this div tag <div id="ReloadThis"></div> and get what from the data.php file and reload it into this div tag, that is awesome but my issue that in the PHP file contains a posted data when it reload the posted data is lost and shows me Undefined index . I searched on google how to do this, I found to use $.post in AJAX, So is $.post is useful for that issue, if yes so how can I use it?

Thanks :)

HTTP is a stateless protocol which means the server is not required to save the data sent by the previous request. But a number of ways - independent of HTTP obviously - are at your disposal to save that data and retrieve it when you want.

Now if i understood the question right, you expect data to available back into $_POST on the second request. Well, it won't be! $_POST will hold the data sent to it (usually from a form or ajax) for the current request only. If it kept saving results for ever request to the server, things would get quickly out of hands. There are other mechanisms at your disposal to save the data from easy to somewhat hard:

  • $_GET: You can save data across pages using the url and access them via the $_GET array. This should be easy but very cumbersome and not a recommended method for data persistence.
  • session variable : as mentioned you can't save data in $_POST for more than one request. But you can persist data in $_SESSION for some time (it depends on your configuration but on an clean PHP installation that should be around 24 minutes see here ). You can change that value to something you want, see here . Session variables are usually useful if you want to implement some sort of user specific page like in facebook, gmail,... Learm more about sessions from the documentation .
  • Files : you can save you data into files on your web server and read those files when you need the data back. But most web apps are complex and require too much structure and trying to put such complex data into a custom file is more than inconvenient, it's madness.
  • Database : you have already mentioned this, but if you were properly using it, you shouldn't have run into much trouble. Basically, you get data from a page and you'll have that data in $_POST (if that's the method you're using) then save the data in a database(eg a mysql database). Now this is where your data.php comes in: data.php might not use $_POST! As a matter of fact you're using $_GET. It will have to fetch the data you saved in the database and send it back to you.

This answer goes beyond your original question because it seemed as if you don't have enough knowledge about persistence yet. About $.post : it is a jQuery method that allows you to get data from the server using POST method instead of the usual GET method, but it doesn't get data that's in the $_POST array, on the other hand you can pass something to $_POST and the server script can act on that data. That would be some parameters the script that interacts with the database needs. See the documentation for more information.


META

I have not given any code sample to assist you because the code sample you also gave is unusable for people to give answers. Since you're new, the following pages will be useful to help you ask better questions:

If English is not your primary language, you can mention it and participate in your questions' edit while others are trying to improve them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM