Hello,
I have a problem with Ajax. I'm totally noob with Ajax, and I apologize for such a stupid question.
I have a list of elements (loaded by a db) that I want to manage, i.e. Remove, Modify their name..; I want to use ajax to change db and the list.
But I want that the page is modified only AFTER the db has been modified. I can modify the page before the db is modified but it's not what I want.
That's my code:
function setXMLHttpRequest() {
var xhr = null;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
return xhr;
}
function modifyCat(n,newN){
xhrObj = setXMLHttpRequest();
var url = "modifyCat.php?action=modify&cat="+n+"&newCat="+newN;
xhrObj.open("GET", url, true);
links = document.getElementById("cat").getElementsByTagName("a");
updatePage(links);
xhrObj.send(null);
}
function updatePage(links) {
if (xhrObj.readyState == 4) {
var risp = xhrObj.responseText;
//code that works... if not put inside this if!
}
xhrObj.send(null);
}
ModifyCat.php is
//...
else if($action='modify'){
$n = cleanSimpleString($_GET['cat']);
$nN = cleanSimpleString($_GET['newCat']);
$qry = "UPDATE Categorie Set Nome='$nN' WHERE Nome='$n'";
$check = mysql_query($qry) or $db=0;
}
As I understand if (xhrObj.readyState == 4)
should do want I'm asking. Instead with that If nothing happens (in the page, the php is correctly loaded). Without that If the page is correctly reloaded but while the db is working..
edit.
I would like to do that without framework, I think it's a simple thing that can be solved simply.
Thank you.