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 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢