AJAX POST not working


AJAX POST not working

我正在创建一个注册表单,当试图通过JavaScript将表单变量发送到PHP文件时,出现了问题,因为我的PHP文件中的所有$_POST变量都是NULL,我已经用if语句测试了这一点,我只想知道为什么当我使用AJAX时变量为NULL,但当我只是使用一个简单的提交变量有值。

HTML

<form name = "reg" method="POST" action="insert.php">
    <table>
        <tr>
            <td class="ule">First Name</td>
            <td><input type = "text" name="firstnames" id="firstnames"></input></td>
        </tr>
        <tr>
            <td class="ule">Last Name</td>
            <td><input type = "text" name="lastnames" id="lastnames"></input></td>
        </tr>
        <tr>
            <td class="ule">Email</td>
            <td><input type = "text" name="emails" id="emails"></input></td>
        </tr>
        <tr>            
            <td class="ule">User Name</td>
            <td><input type = "text" name = "usernames" id="usernames"></input></td>
        </tr>
        <tr>
            <td class="ule">Password</td>
            <td><input type = "password" name = "passwords" id="passwords"></input></td>
        </tr>
    </table>
    <div class="regButton">
        <button type = "button" onclick="createXMLrequest()" >Register</button>
    </div>
</form>
AJAX

function createXMLrequest(){
    var xmlHTTPreq;
    var name = encodeURIComponent(document.getElementById('firstnames').value);
    var lastname = encodeURIComponent(document.getElementById('lastnames').value);
    var myemail = encodeURIComponent(document.getElementById('emails').value);
    var users = encodeURIComponent(document.getElementById('usernames').value);
    var passwords = encodeURIComponent(document.getElementById('passwords').value);
    var params = "firstnames="+name+"&lastnames="+lastname+"&emails="+myemail+"&usernames="+users+"&passwords="+passwords;
var url = "insert.php";
    if (window.XMLHttpRequest) {
    try {
        xmlHTTPreq = new XMLHttpRequest();
    } catch(e) {
        xmlHTTPreq = false;
    }
} else {
    try{
        xmlHTTPreq = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(e){
        xmlHTTPreq = false;
    }
}
xmlHTTPreq.open("POST", url, true);
xmlHTTPreq.onreadystatechange = function()
{
    if(xmlHTTPreq.readyState == 4 && xmlHTTPreq.status == 200){
        document.getElementById('myDiv').innerHTML = xmlHTTPreq.responseText;
    }
}
xmlHTTPreq.setRequestHeader("Content-type","text/plain");
xmlHTTPreq.send(params);
}
PHP

<?php
session_start();
$db = sqlite_open('my_database.db', 0666, $error);
$firstnames = $_POST["firstnames"]; //potential problem area
$lastnames = $_POST["lastnames"];
$emails = $_POST["emails"];
$usernames = $_POST["usernames"];
$passwords = $_POST["passwords"];
$query_check = "SELECT * FROM User WHERE Username = '.$usernames'";
$result = sqlite_query($db,$query_check);
$elements = sqlite_fetch_array($result,SQLITE_ASSOC);
$num = sizeof($elements);
if ($num > 1)
{
    echo "Username already exists pick another one";
}
else
{
$query = "INSERT INTO User (firstName, lastName, Email, Username, Password) VALUES ('.$firstnames','.$lastnames','.$emails','.$usernames','.$passwords')";
    if($firstnames != NULL && $lastnames != NULL && $emails != NULL && $usernames != NULL && $passwords != NULL)
    {
        $_SESSION['registerSession'] = $usernames;
        echo "Thank you for registering " .$_SESSION['registerSession'];
        $result = sqlite_query($db, $query);    
    }
    else
    {
        echo "<h1 style=color:red;>Fill out required fields</h1>";
    }   
}
?>
xmlHTTPreq.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //<-----

你打错字了:

 .... +myemail+"&user="+usernames+"&passwords="+passwords; ....

应为

 .... +myemail+"&usernames="+usernames+"&passwords="+passwords; ....

目前,$usernames将永远是NULL,这就是为什么你结束在ELSE语句…你需要设置正确的请求头:

xmlHTTPreq.setRequestHeader("Content-type","application/x-www-form-urlencoded");