2014-10-11 65 views
-1

好吧,所以我想让PHP将用户数据保存到XML中,然后将用户转发到主页上..但是当JavaScript运行时,它不会在最后抓取回显脚本以及header();AJAX没有通过javascript呼应

的PHP我愿重申

echo "<b>Congratulations registration was Successful.<br /> Your Customer ID is: $id4</b>"; 
      echo "<br/>"; 
      echo "redirected automatically in 7 seconds"; 
      header("Refresh:7; buyonline.htm", true, 303); 
      $doc->save("data/customer.xml"); 
     } 

以及JavaScript函数

var firstname = document.getElementById("firstName").value; 
      var lastname = document.getElementById("lastName").value; 
      var email = document.getElementById("email").value; 
      var password = document.getElementById("password").value; 
      var password2 = document.getElementById("password2").value; 
      var number = document.getElementById("pNumber").value; 
      var type = ""; 
      var input = document.getElementsByTagName("input"); 
       xHRObject.open("GET", "testregristation.php?firstName=" + firstname + "&lastName=" + lastname + "&email=" + email + "&password="+password+"&pNumber="+number, true); 
       xHRObject.onreadystatechange = function() { 
        if (xHRObject.readyState == 4 && xHRObject.status == 200) 
        { 
         document.getElementById('information').innerHTML = xHRObject.responseText; 
        } 
       xHRObject.send(null); 

显然,这些都是小片段,但我不明白为什么它不会发送到屏幕的响应它显示了响应在“萤火虫”..我的PHP也做了电子邮件检查,以确保它没有被使用,并且完美的回声。

+0

'xHRObject.status == 200' - 你不必在你的响应200个状态码:'标题(“刷新:7; buyonline.htm“,true,303);'它是303 - 你永远不会输入javascript的部分,你实际上设置了innerhtml,更好地使用console.log来进行调试 - 还*确保* PHP错误记录正确设置,以防万一你得到一些标题警告等 – hakre 2014-10-11 13:05:38

+0

好吧我把它改为200 ...它现在弹出与回声谢谢你...但我也有头问题不发送用户?buyonline.htm – 2014-10-11 13:08:26

+0

头文件根本没有发送任何用户,所以它不应该,你的意思是说用户正在重定向?我想是这样,所以让我把它与重复的文件关联起来(了解现在正确的错字材料,你说) – hakre 2014-10-11 13:20:17

回答

1

我还没有真正测试过你的代码,以确保我要说的是100%正确。 我从来没有试图在ajax响应中以这种方式使用标题。 但是,我想如果你想重定向用户只是使用JavaScript的。

PHP:

echo "<b>Congratulations registration was Successful.<br /> Your Customer ID is: $id4</b>"; 
echo "<br/>"; 
echo "redirected automatically in 7 seconds"; 
$doc->save("data/customer.xml"); 

的Javascript:

if (xHRObject.readyState == 4 && xHRObject.status == 200) 
{ 
    document.getElementById('information').innerHTML = xHRObject.responseText; 
    setTimeout(function() 
    { 
     window.location.pathname = "/buyonline.htm"; 
    }, 7000 /* 7 seconds */); 
} 

编辑: 现在,因为你使用JavaScript重定向的页面,你也可以倒计时秒:

PHP:

echo "<b>Congratulations registration was Successful.<br /> Your Customer ID is: $id4</b>"; 
echo "<br/>"; 
echo "redirected automatically in <span id="redirect-countdown">7 seconds</span>"; 
$doc->save("data/customer.xml"); 

的Javascript:

if (xHRObject.readyState == 4 && xHRObject.status == 200) 
{ 
    document.getElementById('information').innerHTML = xHRObject.responseText; 

    var countdown = document.getElementById("redirect-countdown"); 

    var count = 8; 
    var interval = setInterval(function() 
    { 
     count--; 

     countdown.innerText = count + " second" + (count > 1 ? "s" : ""); 

     if(count <= 0) 
     { 
      clearInterval(interval); // Kill the interval timer 
      window.location.pathname = "/buyonline.htm"; // Redirect to that page 
     } 

    }, 1000); 
} 
+0

我从来没有改变过一个使用JavaScript的网页,但这是行不通的...我没有复制它的单词只是一个字incase我本来是想改变一个默认t值。 – 2014-10-11 13:10:26

+0

我刚刚给出了主要想法,我没有测试过这个代码。但请注意,window.location.href是完整的地址。尝试使用路径名代替:window.location.pathname =“/buyonline.htm”; – Ido 2014-10-11 13:12:48