duanlian1320 2010-07-06 10:29
浏览 41

无法获得Ajax的工作原理

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.

  • 写回答

3条回答 默认 最新

  • duansengcha9114 2010-07-06 10:34
    关注

    It would save you a lot of headache if you just used jquery - it can be as easy as doing this:

    $.get("modifyCat.php", { action: "modify", cat: n, newCat: newN },
       function(data){
         alert("Data Loaded: " + data);
       });
    

    http://api.jquery.com/jQuery.get/

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化