【问题标题】:How to Use xmlhttp.responseText in If condition?如何在 If 条件下使用 xmlhttp.responseText?
【发布时间】:2013-11-03 23:05:57
【问题描述】:

这是我的 save_custLog_data.php 文件。

<?php
    $a = $_GET['custEmail'];
    $b = $_GET['pswrd'];
    $file = '/home/students/accounts/s2090031/hit3324/www/data/customer.xml';

    if(file_exists($file))
    {

        $xml = new SimpleXMLElement($file, null, true);

        foreach($xml->children() as $child)
        {

            if((($child->EmailAddress) == $email) && (($child->Password) == $password))
            {

                if(isset($session))
                {
                    $session = (string)$child->CustId;
                    $_SESSION['cust_id'] = $session;
                    echo"Welcome";

                }
        break;

             }
     else
             {
                    echo"Invalid Email or Password!! Please Enter again";
                    exit();
      }
        }

}
else
{
    echo"XML file you are trying to access doesn't exist";
}


?>

上面是我的php页面。下面你可以看到我的html页面。但我只会添加嵌入的 javascript 部分。

<script>
    function validate()
{
    var email = "";
    var pswrd = "";

    email = document.getElementById("txtEmail").value;
    pswrd = document.getElementById("txtPaswrd").value;


    if((email != "") && (pswrd != ""))
    {
         if (window.XMLHttpRequest)
         {// code for IE7+, Firefox, Chrome, Opera, Safari
           xmlhttp=new XMLHttpRequest();
         }
         else
         {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                var msg = xmlhttp.responseText;
                if(msg == "Welcome")
                {
                    window.location.assign("buying.htm");//this method call doesn't work
                }
                else
                {
                    alert(msg);
                }


            }
         }

         xmlhttp.open("GET","save_custLog_data.php?custEmail="+email+"&pswrd="+pswrd,true);
         xmlhttp.send();

    }
    else
    {
        alert("Loging form can't be empty");
    }

}
</script>

所以我的问题是为什么 window.location.assign("buying.htm") 方法调用不起作用

【问题讨论】:

  • 命令本身正在工作 - 你确定条件得到满足吗?您是否使用调试器或 console.log() 进行了检查?
  • 我认为 if 条件根本不起作用。只有“其他”部分有效。所以我改为重定向到指定的页面,我收到一条警报消息。
  • 我从这里找到了答案stackoverflow.com/questions/3000649/…

标签: javascript php html ajax


【解决方案1】:

添加斜线

window.location.assign('/buying.htm');

【讨论】:

  • 我试过了,但还是不行。顺便说一句,我稍微编辑了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多