[英]Pass custom error message AJAX
在我的index.php頁面上,我有一個表單。 提交表單后,它進入process.php,該文件運行以下代碼:
$(document).ready(function() {
$('#login_button').click(function()
{
event.preventDefault();
var formData = {
'email' : $('input[name=email]').val(),
'password' : $('input[name=password]').val()
};
$.ajax({
type : 'POST',
url : 'ajax/proclogin.php',
data : formData,
dataType : 'json',
success : function(data)
{
if (data.success == false)
{
if(data.errors.email)
{
toastr.error('Email is blank. Type in an email.');
}
else if(data.errors.password)
{
toastr.error('Password input is blank. Type in a password.');
}
}
else
{
toastr.success('Works!', 'WooHoo!');
}
}
});
});
});
然后執行以下代碼,即proclogin.php頁面:
<?php
// proc(ess)login.php
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
// validate the variables ======================================================
// if any of these variables don't exist, add an error to our $errors array
if (empty($_POST['email']))
{
$errors['email'] = 'Email field is blank.';
}
if (empty($_POST['password']))
{
$errors['password'] = 'Password field is blank.';
}
// return a response ===========================================================
// if there are any errors in our errors array, return a success boolean of false
if(empty($errors))
{
// if there are no errors process our form, then return a message
$connection = mysqli_connect("****","*****","*****","*****");
$email = mysqli_real_escape_string($connection, $_POST['email']); # Define email field
$input = mysqli_real_escape_string($connection, $_POST['password']); # Define password field
$query = mysqli_query($connection, "SELECT `Email`, `Password` FROM users WHERE Email='$email' LIMIT 1"); # Query what we need
$row = mysqli_fetch_array($query); # Fetch what we need
$p = $row['Password']; # Define fetched details
$email = $row['Email']; # Define fetched details
if(password_verify($input, $p)) # Verify input password matches hashed password in the DB.
{
#It matches, let's set a session and redirect them to the dashboard.php page.
//$_SESSION['SID'] = $email;
$data['success'] = true;
$data['message'] = 'Success';
}
else
{
$data['success'] = false;
$data['message'] = 'failed';
}
}
else
{
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
}
// return all our data to an AJAX call
echo json_encode($data);
?>
當電子郵件字段為空白時,您將獲得在process.js頁面中編寫的錯誤消息。 當密碼字段為空白時,您將獲得在process.js頁面中寫入的錯誤消息。
從proclogin.php都不會推送任何錯誤消息。 我對此不太在意,令我困擾的是,“失敗”的人被迫通過通知他們的憑證錯誤。 我玩過這些代碼,並嘗試匹配上面的內容,但是我無法使其正常工作。
我還創建了$ errors ['deny'] =“錯誤的詳細信息”; 並嘗試將其作為data.errors.deny推送到process.js頁面上-這是正確的方法嗎? (是AJAX / jQuery的新功能)。
您必須在function參數中傳遞事件。 嘗試這個:
$(document).ready(function() {
$('#login_button').click(function(event){
event.preventDefault();
var formData = {
'email' : $('input[name=email]').val(),
'password' : $('input[name=password]').val()
};
$.ajax({
type : 'POST',
url : 'proclogin.php',
data : formData,
dataType : 'json',
success : function(data)
{
if (data.success == false)
{
if(data.errors.email)
{
toastr.error(data.errors.email);
}
else if(data.errors.password)
{
toastr.error(data.errors.password);
}
}
else
{
toastr.success('Works!', 'WooHoo!');
}
}
});
});
});
使用最新的toastr和jquery,可以正常工作
在proclogin.php中,您需要修改以下部分代碼:
if(password_verify($input, $p)) # Verify input password matches hashed password in the DB.
{
#It matches, let's set a session and redirect them to the dashboard.php page.
//$_SESSION['SID'] = $email;
$data['success'] = true;
$data['message'] = 'Success';
}
else
{
$data['success'] = false;
$data['success'] = false;
$data['errors']['password'] = 'Pass not match';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.