2013-11-03 76 views
1

這是我的save_custLog_data.php文件。如何在If條件中使用xmlhttp.responseText?

<?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」)方法調用不起作用

+0

本身是工作的命令 - 你確定條件得到滿足嗎?你用debugger或console.log()檢查過嗎? –

+0

我認爲if條件根本不起作用。只有「其他」部分正在工作。所以,而是我重定向到指定的網頁,我得到一個警報消息。 –

+0

我從這裏找到答案http://stackoverflow.com/questions/3000649/trim-spaces-from-start-and-end-of-string –

回答

0

前面加上斜槓

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

我試過這個,但仍然無法正常工作。順便說一句,我編輯了一下這個問題。 –