$ .post回调函数不起作用

时间:2014-04-19 10:00:38

标签: jquery ajax

我有一个表单,我想通过ajax处理代码像这样的

$(function()   
{
var ck_username = /^[A-Za-z0-9_]{3,20}$/;
var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}
    (?:\.[a-z]{2})?)$/i 
var ck_password =  /^[A-Za-z0-9!@#$%^&*()_]{6,20}$/;
$('#User').submit(function(event)
{
    var formData = {
        'email' : $("#Reg #email").val(),
        'username': $("#Reg #username").val(),
        'password': $("#Reg #password").val(),
        'password1': $("#Reg #password1").val()
    };
    if(ck_email.test(formData['email']) &&
            ck_username.test(formData['username']) &&
            ck_password.test(formData['password']) &&formData['password'] ==
             formData['password1'])
    {
    // process the form
    $.ajax({
        type        : 'POST',
        url         : $("#User").attr('action'),
        data        : formData, 
        dataType    : 'json' 
    })
        // using the done promise callback
        .done(function(data) {

            // log data to the console so we can see
            console.log(data); 


            // here we will handle errors and validation messages
        })
        .fail(function(data) {

            // show any errors
            // best to remove for production
            console.log(data);
        });         
    }
    // stop the form from submitting the normal way and refreshing the page
    event.preventDefault();
});
});

和处理这些数据的php如下

<?php
$email = mysql_real_escape_string(trim($_POST['email']));
$name = mysql_real_escape_string(trim($_POST['username']));
$password = mysql_real_escape_string(trim($_POST['password']));


$data = array (
'name' => $name,
'email' => $email,
'password' => $hash,
'usererror' => false,
'emailerror' => false,
'status' => false,
);

$checkUser = mysql_query("SELECT * FROM  `table_name` WHERE `name` = '$name'");
$checkmail = mysql_query("SELECT * FROM  `table_name` WHERE `email` = '$email'");

if(mysql_num_rows($checkUser) > 0 || mysql_num_rows($checkmail) > 0)
{
if(mysql_num_rows($checkUser) > 0) $data['usererror'] = true;
if(mysql_num_rows($checkmail) > 0) $data['emailerror'] = true;
}
else
{
$result = mysql_query("INSERT INTO `table_name` (`name` ,`email` ,`password`)
    VALUES  ('$name', '$email', '$hash')");
$id = mysql_insert_id();
if($result)
{
    $data['status'] = true;
    // start session
    session_start();

    //session variable
    $_SESSION['id'] = $id;
    $_SESSION['username'] = $name;
    $_SESSION['category'] = 'animals';

}       
}
echo json_encode($data);
?>

当我提交提交按钮时,回调函数没有运行请帮我解决这个问题 提前致谢

这是我的HTML

<form method="post" id="User" action="myphpscript.php">
  <label>Email: </label><br/> 
  <input type="text" name="email" id="email"/>

  <label>Username: </label><br/> 
  <input type="text" name="name" id="username"/>

  <label>Password: </label><br/> 
  <input type="password" name="password" id="password"/>

<label>Re-enter Password: </label><br/> 
<input type="password" name="password1" id="password1"/>


<input type="submit" value=" Register " id='submit'/>
</form>

当我添加     .error(function(){alert(&#34; error&#34;);});

在我的帖子请求结束时它会提醒错误

2 个答案:

答案 0 :(得分:0)

您期待json回复

dataType    : 'json' 

因此,您必须使用以下命令在PHP代码中设置正确的标题:

header('Content-type: application/json');

此外,我认为您也有一些PHP错误,$hash未声明

$data = array (
'name' => $name,
'email' => $email,
'password' => $password,//EDIT
'usererror' => false,
'emailerror' => false,
'status' => false,
);

答案 1 :(得分:0)

试试这个

<input type="button" id="submitForm" >

更改

$('#User').submit(function(event)
{

$('#submitForm').click(function(event)
{