在我的注册表单中使用ajax + php。有2个验证:JS端前端和PHP端后端。在PHP方面创建了名为response
的特殊函数:它将PHP端错误作为JSON数据发送。
问题是我无法从PHP方面得到任何回复。
分析firebug中的页面:收到错误消息responseData is null
。 (responseData = jQuery.parseJSON(data)
)
JS部分看起来像
//check the form is not currently submitting
if ($(this).data('formstatus') !== 'submitting') {
var form = $(this),
formData = form.serialize() + '&formID=' + form.attr('id'),
formUrl = form.attr('action'),
formMethod = form.attr('method');
//add status data to form
form.data('formstatus', 'submitting');
if (validate()) {
//send data to server for validation
$.ajax({
url: formUrl,
type: formMethod,
data: formData,
success: function (data) {
//setup variables
var responseData = jQuery.parseJSON(data),
cl, text;
//response conditional
switch (responseData.status) {
case 'error':
cl = 'error';
text = responseData.message;
break;
case 'success':
cl = 'success';
text = 'Qeydiyyat uğurla başa çatdı';
break;
}
$.notifyBar({
cls: cl,
html: text
});
}
});
}
form.data('formstatus', 'idle');
}
这是PHP部分
<?php
require '../common.php';
function checkIfEmailExists($email, $stmt)
{
if ($stmt = $db->prepare("SELECT id FROM TABLE WHERE email=? LIMIT 1")) {
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($count);
$stmt->close();
}
return ($count > 0 ? true : false);
}
if ($_POST['formID'] == 'signup_form') {
// Setting vars
$lname = $_POST['lname'];
$fname = $_POST['fname'];
$mname = $_POST['mname'];
$email = $_POST['email'];
$pass = $_POST['pass'];
$confirm_pass = $_POST['confirm_pass'];
//=====================
//Server side validation >>
//First name, middle name, last name check >>
if (!$lname) {
response('error', 'Familiyanı daxil edin');
}
if (!$fname) {
response('error', 'Adı daxil edin');
}
if (!$mname) {
response('error', 'Atanızın adını daxil edin');
}
//<<
//Pass check >>
if (strlen($pass) > 2) {
if ($pass == $confirm_pass) {
return true;
} else {
response('error', 'Şifrənin təkrarlanmasında səhv');
}
} else {
response('error', 'Şifrədə simvolların sayı 4-dən çox olmalıdır');
}
//<<
//email validation >>
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (!checkIfEmailExists($email, $stmt)) {
return true;
} else {
response('error', 'Bu ünvanla qeydiyyata alınmış başqa istifadəçi var.');
}
} else {
response('error', 'Email ünvanını düzgün daxil edin');
}
//<<
// Create statement object
$stmt = $db->stmt_init();
// Create a prepared statement
if ($stmt->prepare("INSERT INTO `users` (`fname`, `mname`, `lname`, `email`, `pass`, `reg_dt`) VALUES (?, ?, ?, ?, ?, NOW())")) {
// Binding vars
$rc = $stmt->bind_param('sssss', $fname, $lname, $mname, $email, $pass) or die('bind_param() failed: ' . htmlspecialchars($stmt->error));
// Execute query
$rc = $stmt->execute();
if ($rc) {
response('success', 'Qeydiyyat uğurla başa çatdı');
} else {
response('error', htmlspecialchars($stmt->error));
}
// Close statement object
$stmt->close();
} else {
response('error', htmlspecialchars($dv->error));
}
}
else {response('error', 'Qeydiyyatda problem');}
//return json response
function response($status, $message)
{
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
die();
}
?>
答案 0 :(得分:1)
您需要添加
dataType: "json",
在$ .Ajax方法中。