来自php的AJAX responseText不起作用

时间:2012-09-28 01:42:00

标签: php javascript ajax

我正在尝试使用AJAX创建一个用户注册表单,我对此很新,所以我对我正在做的事情没有多少经验。我的问题是没有让php工作或AJAX发送文件,它回来时responseText没有任何价值。我想要它,以便responseText将是PHP回声。我想我沿着正确的路线做什么呢?请帮忙。也很抱歉这个问题布局不好,我很快就会收拾它。

HTML:

<div id="registration">
    <form method="post" action="" onsubmit="formValidate()">
        <!-- Apply Javascript for form validation and change password property -->
        <div id="title">First Name:</div>
        <input type="text" name="firstname" id="firstname" value="First Name"/>
        <div id="title">Surname:</div>
        <input type="text" name="surname" id="surname" value="Surname"/>
        <div id="title">Address:</div>
        <input type="text" name="address" id="address" value="Address Line 1"/>
        <input type="text" id="town_city" name="town_city" value="Town/City"/>
        <input type="text" name="postcode" id="postcode" value="Postcode" />
        <input type="text" name="country" id="country" value="Country" />
        <div id="title">Email:</div>
        <input type="text" name="email" id="email" value="Email" />
        <div id="title">Password:</div>
        <input  type="text" name="password" id="password" value="Password" />
        <div id="title">Verify Password:</div>
        <input type="text" name="vPassword" id="vPassword" value="Passwloord" />
        <div id="status"></div>
        <input type="submit" name="submit" value="Register" />
    </form>
</div>

JS:

function formValidate() {
    if (window.XMLHttpRequest) {
        xml = new XMLHttpRequest();
    } else {
        xml = new ActiveXObject('Microsoft.XMLHTTP');
    }

    var url = 'php/validateUser.inc.php';
    var fn = document.getElementById('firstname').value;
    var ln = document.getElementById('surname').value;
    var add = document.getElementById('address').value;
    var tc = document.getElementById('town_city').value;
    var pCode = document.getElementById('postcode').value;
    var country = document.getElementById('country').value;
    var email = document.getElementById('email').value;
    var pass = document.getElementById('password').value;
    var vPass = document.getElementById('vPassword').value;
    var userInput = "firstname=" + fn + "&surname=" + ln + "&address=" + add + "&town_city=" + tc + "&postcode=" + pCode + "&country" + country + "&email=" + email + "&password=" + pass + "&vPassword=" + vPass;

    xml.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xml.open('POST', url, true);

    xml.onreadystatechange = function() {
        if (xml.readyState == 4) {
            if (xml.status == 200) {
                var return_data = xml.responseText;
                document.getElementById("status").innerHTML = return_data;
            } else {
                alert("GNAH!");
            }
        }

        xml.send(userInput);
        document.getElementById("status").innerHTML = "Processing....";
    }

在我完成这部分之后,我希望PHP的回显出现在状态div中但是它们出现在屏幕的左上角,而“Processing ....”却没有。任何建议请知道这是错误的文件。

PHP:

<?php
if (isset($_POST['submit'])) {

    if (isset($_POST['firstname'], $_POST['surname'], $_POST['address'], $_POST['town_city'], $_POST['postcode'], $_POST['country'], $_POST['email'], $_POST['password'], $_POST['vPassword'])) {
        $fName = mysql_real_escape_string($_POST['firstname']);
        $lName = mysql_real_escape_string($_POST['surname']);
        $addr = mysql_real_escape_string($_POST['address']);
        $town_city = mysql_real_escape_string($_POST['town_city']);
        $pCode = mysql_real_escape_string($_POST['postcode']);
        $country = mysql_real_escape_string($_POST['country']);
        $email = mysql_real_escape_string($_POST['email']);
        $pass = mysql_real_escape_string($_POST['password']);
        $pass_hash = md5($pass);
        $vPass = mysql_real_escape_string($_POST['vPassword']);

        if (!empty($fName) && !empty($lName) && !empty($addr) && !empty($town_city) && !empty($pCode) && !empty($country) && !empty($email) && !empty($pass) && !empty($vPass) && $pass == $vPass) {
            $query = "INSERT INTO `usertable` (user_id, fName, lName, Address, town_city, pCode, Country, Email, Password) VALUES ('', '$fName', '$lName', '$addr', '$town_city', '$pCode', '$country', '$email', '$pass')";
            $query_run = mysql_query($query);
            if ($query_run) {
                header('Location: login.php');
            } else {
                echo mysql_error();
            }
        } else {
            echo 'Please fill in all fiels and make sure both passwords match.';
        }
    } else {
        echo mysql_error();
    }
}
?>

1 个答案:

答案 0 :(得分:2)

您没有为userInput变量中的“提交”表单字段提供值,因此测试:

if ($_POST['submit'])

失败。将作业更改为:

var userInput = "submit=Register&firstname="+fn+...