Javascript 引起PHP注意的原因:未定义索引:?

Javascript 引起PHP注意的原因:未定义索引:?,javascript,php,jquery,ajax,mobile,Javascript,Php,Jquery,Ajax,Mobile,我在jQuery移动应用程序中有一个用户注册表单,但当我通过$.ajax调用提交表单时,它只插入一个空行,错误日志显示: [01-Dec-2013 13:27:13] PHP Notice: Undefined index: fname in /home2/hedonsof/public_html/tcob/php/register.php on line 10 [01-Dec-2013 13:27:13] PHP Notice: Undefined index: lname in /h

我在jQuery移动应用程序中有一个用户注册表单,但当我通过$.ajax调用提交表单时,它只插入一个空行,错误日志显示:

[01-Dec-2013 13:27:13] PHP Notice:  Undefined index:  fname in /home2/hedonsof/public_html/tcob/php/register.php on line 10
[01-Dec-2013 13:27:13] PHP Notice:  Undefined index:  lname in /home2/hedonsof/public_html/tcob/php/register.php on line 11
[01-Dec-2013 13:27:13] PHP Notice:  Undefined index:  username in /home2/hedonsof/public_html/tcob/php/register.php on line 12
[01-Dec-2013 13:27:13] PHP Notice:  Undefined index:  password in /home2/hedonsof/public_html/tcob/php/register.php on line 13
[01-Dec-2013 13:27:13] PHP Notice:  Undefined index:  email in /home2/hedonsof/public_html/tcob/php/register.php on line 14
我的索引:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery Mobile Web App</title>
<link href="lib/BlackBerry-JQM-all-1.0.0.css" rel="stylesheet" type="text/css"/>
<link href="style/main.css" rel="stylesheet" type="text/css"/>
<script src="lib/BlackBerry-JQM-all-1.0.0.js" type="text/javascript"></script>
<script src="ui/jquery.ui.map.js" type="text/javascript"></script>
<script src="ui/jquery.ui.map.services.js" type="text/javascript"></script>
<script src="ui/jquery.ui.map.extensions.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>
<script src=http://maps.googleapis.com/maps/api/js?sensor=false type="text/javascript"></script>
</head>
<body>
<div data-role="page" id="landing">
  <div data-role="header"> </div>
  <div data-role="content">
    <div id="map_
    canvas" style="height:600px"> </div>
    <div id="info"> </div>
  </div>
  <div data-role="footer">
    <div data-role="actionbar"> <a id="help" data-role="tab"> <img src="img/ic_help.png"/>
      <p>Help</p>
      </a> <a id="chat" data-role="tab"> <img src="img/ic_textmessage.png"/>
      <p>Chat</p>
      </a> <a id="add" data-role="tab" href="#add"> <img src="img/ic_add.png"/>
      <p>Add </p>
      </a> <a id="settings" data-role="tab" href="#register"> <img src="img/Core_applicationmenu_icon_settings.png" alt="" />
      <p>Settings</p>
      </a> </div>
  </div>
</div>
<div data-role="page" id="register">
  <div data-role="header"> </div>
  <div data-role="content">
    <div class="BB10Container">
      <form id="adduser">
<label for="fname">First Name</label>
        <input type="text" name="fname" id="fname" placeholder="John"/>
        <label for="lname">Last Name</label>
        <input type="text" name="lname" id="lname" placeholder="Doe"/>
        <label for="username">Username</label>
        <input type="text" name="username" id="username" placeholder="Username"/>
        <label for="basic">Password</label>
        <input type="password" name="password" id="password" placeholder="Password"/>
        <label for="verpass">Repeat Password</label>
        <input type="password" name="verpass" id="verpass" placeholder="Password"/>
        <label for="regemail">Email</label>
        <input type="email" name="email" id="email" placeholder="your@email.com"/>
        <input type="submit" data-role="button" data-inline="true" data-icon="check" value="Submit" id="regsubmit">
</form>

    </div>
    <div id="info"> </div>
  </div>
  <div data-role="footer">
    <div data-role="actionbar"> <a id="help" data-role="tab"> <img src="img/ic_help.png"/>
      <p>Help</p>
      </a> <a id="chat" data-role="tab"> <img src="img/ic_textmessage.png"/>
      <p>Chat</p>
      </a> <a id="add" data-role="tab" href="#add"> <img src="img/ic_add.png"/>
      <p>Add </p>
      </a> <a id="settings" data-role="tab" href="#settings"> <img src="img/Core_applicationmenu_icon_settings.png" alt="" />
      <p>Settings</p>
      </a> </div>
  </div>
</div>
</div>
<script type="text/javascript">

 $(document).ready(function() {
    $("#regsubmit").click(function(){

        var formData = $("#adduser").serialize();
 alert(formData);   
        $.ajax({
            type: "POST",
            url: "register.php",
            cache: false,
            data: fname: fname,
                  lname: lname,
                  username: username,
                  password: password,
                  email: email,
            success: onSuccess
        });

        return false;
    }); 
 });

 function onSuccess(data, status)
{
    alert('Success');
}
</script>
</body>
</html>

jQuery移动Web应用程序
名字
姓
用户名
密码
重复密码
电子邮件
$(文档).ready(函数(){
$(“#regsubmit”)。单击(函数(){
var formData=$(“#adduser”).serialize();
警报(formData);
$.ajax({
类型:“POST”,
url:“register.php”,
cache:false,
数据:fname:fname,
lname:lname,
用户名:用户名,
密码:密码,
电邮:电邮,,
成功:成功
});
返回false;
}); 
});
成功时的功能(数据、状态)
{
警惕(“成功”);
}
我的PHP

<?php
require('connect.php');
ini_set('display_errors', 'On');
error_reporting(E_ALL);
try{
$db = mysql_connect($host, $dbusername, $password ) or die(mysql_error());
    mysql_select_db($db_name) or die(mysql_error());

    // Note: You need to verify the data coming in isn't harmful, this SQL pretty much puts anything into the database so make sure to change this!
        $fname = $_POST['fname'];
        $lname =$_POST['lname'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];

        mysql_query("INSERT INTO members (fname, lname, username, password,email) VALUES ('$fname', '$lname','$username','$password','$email')");
        mysql_close($db);
        echo "SUCCESS";
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
        // Note: Log the error or something
    }
?>

从Ajax调用发布到register.php的
数据需要是这样一个JSON(注意在字段周围添加的
{}
):


通过此更改,您将能够访问
$\u POST
关联数组中的字段。这应该解决PHP错误

您可能还想签出此选项,它将自动将表单中的所有表单元素作为$\u POST请求中的键值对发送

您正在发送数据中的空白值。您已将$.POST的数据字段定义为

data: fname: fname,
      lname: lname,
      username: username,
      password: password,
      email: email,
但是您从未设置参数fname、lname等的值。这意味着,当您说
fname:fname
——冒号右侧的fname值是多少?此外,进一步向下发布,参数列表需要包装在
{}
中。你可以在地图上看到一个例子

但是,要解决您的问题,只需将序列化的表单数据作为post对象发送即可

data: formdata

这将发送您在帖子中已经执行的序列化表单。

他已经在使用.serialize()函数,除了向屏幕提示结果之外,他实际上没有对结果做任何事情。这是否回答了您的问题?
$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
data: formdata