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';
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?