dongxiezhuo8852 2013-04-24 11:35
浏览 55
已采纳

第二个ajax请求后的xmlhttp.responseText

i m writing a script using javascript ajax request and php.. i make an ajax call to verify that a user is recognised or not and according to the "response" i want to make a second ajax request either letting him either make his choices if his is already a registered user, or display the already submitted choices

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    var script = document.createElement('script');
        script.innerHTML = xmlhttp.responseText;
        document.getElementsByTagName('head')[0].appendChild(script);
        removeOnclick();
        //document.getElementById("txtHint").innerHTML=
    }
  }
xmlhttp.open("GET","submit_votes.php?q="+str+string,true);
xmlhttp.send();
}

on my php side

i have these lines in case he is recognised

if (mysqli_num_rows($result) > 0) {

while($row = mysqli_fetch_array($result))
  {
    echo "document.getElementById('".$row['option1']."').className += ' colour1';";
    ....
}

but if he is not recognised i have set this check

if (!isset($_GET['option1'])){ //option1 is one of the parameters i pass through "string"
    echo "nodata";
    }

the first call is made passing only the string is set ""

 q="+str+string

while on the second call string is set as option1=A&option2=B etc etc...

the issue is, that i try to alert the xmlhttpresponse to check if the response has been altered, but the resposne remains the same..for instance if he is not recognised i ll get in both responses "nodata" while if i refresh the page and he is recognised, i ll get the document.getElementById('".$row['option1']."').className += ' colour1'; response..

the reason i need this is because i want this part

    var script = document.createElement('script');
        script.innerHTML = xmlhttp.responseText;
        document.getElementsByTagName('head')[0].appendChild(script);
        removeOnclick();
        //document.getElementById("txtHint").innerHTML=

to be executed only if he is recognised..

  • 写回答

1条回答 默认 最新

  • doumangzhen7204 2013-04-25 07:56
    关注

    after considering moonwave99 's comment as well, i ended up with a different solution..

    first of all i found out, that condition check failed, because in the responseText there was an extra white space..so initially i used

    xmlhttp.responseText.trim();
    

    in order to remove unwanted characters...but it still didn't feel right the fact that i was passing js code as string from php back to js.. and i decided to use json.. so on the php side, i used json encode

    $options = array($row['option1'],$row['option2'],$row['option3'],$row['option4'],$row['option5'],$row['option6'],
                        $row['option7'],
                        $row['option8'],
                        $row['option9'],
                        $row['option10']
                        );
    echo json_encode($options);
    

    and on the javascript side i used

        var jsObject = JSON.parse(xmlhttp.responseText);
        var jslength = jsObject.length;
        for (i=0; i<jslength; i++){
            document.getElementById(jsObject[i]).className += ' colour';
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R