如何停止 PHP 脚本的响应消息显示为页面的唯一内容


How do I stop the response message of a PHP script appearing as the only content of the page

所以我有一个简单的注册页面,Javascript请求工作正常,但是当响应被发送回去时,页面位置更改为

/注册新用户.php?Name=Andrew&Surname=Too&userID=0014&passWD=pass

除了响应之外,页面上没有显示任何内容。我尝试手动更改页面位置,但这似乎不起作用

<body>
<div id="container">
    <header>
        <h1>Email Client</h1>
            <a href="login.html">
            <div class="topButton">Log in</div>
            </a>
        <h3>New User Registration</h3>
    </header>
    <div id="body">
        <form name="register" action="RegisterNewUser.php" method"get";>
            First Name:<input type="text" name="Name"/><br/>
            Surname:<input type="text" name="Surname"/><br/>
            User ID:<input type="text" name="userID" id="userID"/><br/>         
            Password:<input type="password" name="passWD" id="pass1"/><br/>
            Re-enter password:<input type="password" id="pass2"/><br/>
            <div id="feedback"></div>
            <!--Inline validation of whether the passwords match-->
            <input type="submit" value="Register" id="regSubmit">
            <div id="formFeedback"></div>
            <!--If "user ID exists" is retuned, make it an alert, e.g. alert("That user ID is         taken, please try another.")-->
        </form>
    </div>
</div>
</body>
</html>
<script>
var xhr=null;
function registrationCheck(){
    if (window.ActiveXObject){
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest){
       xhr = new XMLHttpRequest(); 
    } 
    else {alert("Your browser does not support Ajax");}
    if (xhr!=null){  
         xhr.onreadystatechange=Response;
         xhr.open('POST','RegisterNewUser.php',false);
         xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
             xhr.send("Name=personName&Surname=personSurname&userID=personID&passWD=personpassword");
     }
}
function Response()
{
    if (xhr.readyState==4)
        {
            if (xhr.status==200)
                {
                var response=xhr.responseText;
                if(response == 'registered')
                    {
                        alert(response);
                    }
                else
                    {
                        alert('User already Exists');
                    }                   
                }
            else
                {
                    alert('Error!');
                }
        }
}   
$(document).ready(function(){
    $('#regSubmit').on('change', function(){
            registrationCheck();
        });
    });
</script>

您应该添加"单击"事件而不是"更改"事件并在语句末尾添加"返回错误;"

$(document).ready(function(){
    $('#regSubmit').on('click', function(event){
        registrationCheck();
        return false;
    });
});

您必须阻止默认提交行为。

$(document).ready(function(){
    $('#regSubmit').on('change', function(event){
        registrationCheck();
        event.preventDefault();
        event.unbind();
    });
});