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:
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.
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.