0

我正在尝试使用 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 中,但它们出现在屏幕的左下角,但“处理中……”却没有。请有任何建议,我知道这是错误的文件。

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();
    }
}
?>
4

1 回答 1

2

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

if ($_POST['submit'])

失败。将分配更改为:

var userInput = "submit=Register&firstname="+fn+...
于 2012-09-28T02:49:09.553 回答