通过Ajax提交输入值时PHP未定义的索引

时间:2014-02-07 02:22:54

标签: javascript php ajax

我正在尝试将数据提交到数据库中,但我收到了Undefined Index错误。

我使用isset函数检查我是否获得任何值并使用print_r但我无法获得任何值。

虽然ajax成功发送请求,但php $_POST仍无法获取任何值。它是空的。

这是错误

Notice: Undefined index: email in C:\xampp\htdocs\msaccess\tst.php on line 5

Notice: Undefined index: username in C:\xampp\htdocs\msaccess\tst.php on line 6

Notice: Undefined index: fullname in C:\xampp\htdocs\msaccess\tst.php on line 7

Notice: Undefined index: password in C:\xampp\htdocs\msaccess\tst.php on line 8

Notice: Undefined index: repass in C:\xampp\htdocs\msaccess\tst.php on line 9
INSERT INTO submitform(fullname,email,username,userpassword,datesubmit) VALUES ('','','','','02/07/2014 02:45:53')
Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Field 'submitform.username' cannot be a zero-length string., SQL state S1000 in SQLExecDirect in C:\xampp\htdocs\msaccess\tst.php on line 13
success

这是我的HTML

<div class="form">
                    <div class="form-header">
                        Create Your Account
                    </div>
                    <div class="form-body">
                        <div class="row line">
                            <div class="callit">Full Name: </div>
                            <div class="inputbox"><input class="fullname" type="text" id="fullname" placeholder="Full Name" /></div>
                        </div>
                        <div class="row line">
                            <div class="callit">Email: </div>
                            <div class="inputbox"><input class="email" type="email" id="email" placeholder="Email Address"  /></div>
                        </div>
                        <div class="row line">
                            <div class="callit">Username: </div>
                            <div class="inputbox"><input class="username" type="text" id="username" placeholder="Username" /></div>
                        </div>
                        <div class="row line">
                            <div class="callit">Password: </div>
                            <div class="inputbox"><input class="password" type="password" id="password" placeholder="Password" /></div>
                        </div>
                        <div class="row line">
                            <div class="callit">Retype Password: </div>
                            <div class="inputbox"><input class="repass" type="password" id="repass" placeholder="Retype Password"  /></div>
                        </div>
                    </div>
                    <div class="footer">
                        <button type="button" onclick="vals();">Create Account</button>
                        <div><span id="status"></span></div>
                    </div>
            </div>

这是我的Javascript / Ajax调用

<script type="text/javascript">
        function vals(){
            var e = document.getElementById('email').value;
            var fn = document.getElementById('fullname').value;
            var u = document.getElementById('username').value;
            var ps = document.getElementById('password').value;
            var rep = document.getElementById('repass').value;
            document.getElementById('status').innerHTML = e+' '+fn+' '+u+' '+ps+' '+rep+' ';

            var xmlhttp;
            if(window.XMLHttpRequest){
                xmlhttp = new XMLHttpRequest();
            }
            else{
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState==4 && xmlhttp.status == 200){
                    document.getElementById('status').innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("POST", "tst.php", true);
            xmlhttp.send("email="+e+"&fullname="+fn+"&username="+u+"&password="+ps+"&repass="+rep);
        }
    </script>

以下是tst.php文件代码

<?php
include_once('includes/db.php');


    $email = $_POST['email'];
    $username = $_POST['username'];
    $fullname = $_POST['fullname'];
    $password = $_POST['password'];
    $repass = $_POST['repass'];
    $date = date('m/d/Y H:i:s');
        $sql = "INSERT INTO submitform(fullname,email,username,userpassword,datesubmit) VALUES ('$fullname','$email','$username','$password','$date')";
        echo $sql;
        $rs = odbc_exec($conn, $sql);
        echo 'success';
?>

2 个答案:

答案 0 :(得分:1)

添加

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

上面

        xmlhttp.onreadystatechange = function(){

答案 1 :(得分:1)

更新了Ajax代码

 xmlhttp.open("POST", "tst.php", true);
 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlhttp.send("email="+e+"&fullname="+fn+"&username="+u+"&password="+ps+"&repass="+rep);