AJAX POST无法正常工作

时间:2014-03-28 12:18:50

标签: javascript php ajax sqlite

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

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>";
    }   
}

?>

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你有一个错字:

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

应该是

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

目前,$ usernames总是为NULL,这就是你最终进入ELSE语句的原因...... 您需要设置正确的请求标头:

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