Я изучаю AJAX / JS и после отправки формы я хочу, чтобы AJAX запустил POST и вернул данные.Это было сделано, и данные возвращаются в порядке и успешно, я просто не могу показать функцию «оповещения».Я перенаправлен на свой файл process.php со следующими данными:
{"success":false,"errors":{"email":"Email is required.","password":"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);
?>