[英]AJAX/JS not displaying alert
我正在學習AJAX / JS,一旦提交了表單,我希望AJAX觸發POST並返回數據。 這已經完成,數據又恢復正常,我只是無法顯示“警報”功能。 我通過以下命令重定向到我的process.php:
{"success":false,"errors":{"email":"Email is required.","password":"Password is required."}}
現在,我需要獲取以上內容以使其顯示在“警報”中,例如alert('Password is required');
這是我的“ process.js”形式:
$(document).ready(function()
{
event.preventDefault();
$('form').submit(function(event)
{
var formData = {
'email' : $('input[email=email').val(),
'password' : $('input[password=password').val()
};
$.ajax({
type : 'POST',
url : 'ajax/proclogin.php',
data : JSON.stringify(formData),
dataType : 'json',
encode : true
})
// using the done promise callback
.done(function(data)
{
console.log(data);
if (!data.success)
{
if(data.errors.email)
{
//toastr.error(''+data.errors.email+'', 'Oops!');
alert('Email error');
}
if(data.errors.password)
{
//toastr.error(''+data.errors.password+'', 'Oops!');
alert('Password Error');
}
}
else
{
//toastr.success('Works!', 'WooHoo!');
alert('Works.');
}
});
});
});
這是“ 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 is required.';
if (empty($_POST['password']))
$errors['password'] = 'Password is required.';
// return a response ===========================================================
// if there are any errors in our errors array, return a success boolean of false
if ( ! empty($errors)) {
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
} else {
// 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'] = 'fail';
}
// show a message of success and provide a true success variable
}
// return all our data to an AJAX call
echo json_encode($data);
?>
你有event.preventDefault();
在您的提交處理程序外部,應該在內部。
您用於獲取密碼和電子郵件的選擇器似乎錯誤。
您正在嘗試發送JSON,但嘗試讀取表單數據。
$(document).ready(function(){
$('form').submit(function(event) {
event.preventDefault();// prevent the form from submitting normally
var formData = {
'email' : $('input[type=email').val(), // get the value of the email input
'password' : $('input[type=password').val() // get the value of the password input
};
$.ajax({
type : 'POST',
url : 'ajax/proclogin.php',
data : formData, // send as form data
dataType : 'json',
})
...
您需要檢查是否已設置電子郵件或密碼,然后顯示如下警告消息:
.done(function(data) {
if(data.errors.email) {
alert('email required');
}
else if(data.errors.password) {
alert('password required');
}
else {
alert('done');
}
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
並刪除條件if (!data.success)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.