$('#registerForm').submit(
function()
{
callAjaxSubmitMethod(this);
//event.preventDefault();
}
);
这是我dom准备好的功能。
function callAjaxSubmitMethod(form)
{
$.ajax({
type: "POST",
url: "lib/registration_validate.php",
data: $("#registerForm").serialize(),
success: function(response)
{
alert("s"+response.status);
},
error:function(response)
{
alert("e"+response);
}
});
}
这是实际的功能定义。
我的php内容
<?php
include 'configdb.php';
session_start();
global $connection;
echo "oops";
if(isset($_POST['submit']) && $_POST['email']!='' && $_POST['password']!='')
{ //use empty....
$email= $_POST['email'];
$sql1 = "SELECT * FROM Users WHERE emailID = '$email'";
$result1 = mysqli_query($connection,$sql1) or die("Oops");
if (mysqli_num_rows($result1) > 0)
{
$_SESSION['email_exist']="Email Already Exists";
//die({'status':'Email Already Exists'});
//echo var_dump($_SESSION);
//echo '{status:0}' ;
//exit;
}
}
?>
我没有得到任何警报。如果我启用event.preventDefault()
,我将在mozilla中获得out of memory error
并在Chrome中获得sundefined
提醒。
答案 0 :(得分:1)
使用
if(isset($_POST['submit']) && $_POST['email']!='' && $_POST['password']!='')
{ //use empty....
$email= $_POST['email'];
$sql1 = "SELECT * FROM Users WHERE emailID = '$email'";
$result1 = mysqli_query($connection,$sql1) or die("Oops");
$response = array();
if (mysqli_num_rows($result1) > 0)
{
$_SESSION['email_exist']="Email Already Exists";
$response['status']='Email Already Exists';
}
else{
$response['status']='Allis OK';
}
echo json_encode($response); die;
}
在ajax中,添加:
dataType:'json'
修改强>
有关您的信息,请参阅如何设置dataType:
$.ajax({
type: "POST",
dataType:'json',
url: "lib/registration_validate.php",
data: $("#registerForm").serialize(),
success: function(response)
{
alert("s"+response.status);
},
error:function(response)
{
alert("e"+response);
}
});