AJAX没有通过javascript回应

时间:2014-10-11 12:56:51

标签: javascript php ajax

好的,所以我试图让PHP将用户数据保存到XML,然后将用户转发到主页上..但是当JavaScript运行时,它不会在脚本的末尾抓取回声。 header();

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); 

显然这些都是小片段,但我不明白为什么它不会将响应发送到屏幕上它显示“firebug”中的响应..我的PHP也会进行电子邮件检查以确保其未被使用,并且回声完美。

1 个答案:

答案 0 :(得分: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);
}