$('input[type="submit"]').click(function(){
resetErrors();
$.each($('form input, form textarea, form select'), function(i, v){
if(v.type !== 'submit'){
data[v.name] = v.value;
}
});
$.ajax({
dataType: 'json',
type: 'POST',
url: 'libs/contact.php',
data: data,
success: function(resp){
if(resp === true){
$('form').submit();
if(status == "success"){
console.log('Success')
}
return false;
} else {
$.each(resp, function(i, v){
console.log(i + " => " + v);
var msg = '<label class="error" for"' +i+ '">' +v+ '</label>';
$('input[name="' +i+ '"], textarea[name="' +i+ '"], select[name="' +i+ '"]').addClass('inputTextError').after(msg);
});
var keys = Object.keys(resp);
$('input[name="' +keys[0]+ '"]').focus
}
return false;
},
error: function(){
console.log('There was a problem checking fields');
}
});
return false;
});
});
function resetErrors(){
$('form input, form textarea').removeClass('inputTextError');
$('label.error').remove();
}
Here's my code I have tried everything but still ain't working HELP PLZ I'm doing all my validation on the server I'm just using ajax to display the errors so I would like to display the success message too but it's a little a bit confusing
Here's my PHP code
require_once('phpmailer/PHPMailerAutoload.php');
$errors = [];
if(isset($_POST['name'], $_POST['email'], $_POST['number'], $_POST['message'])){
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
'number' => $_POST['number'],
'message' => $_POST['message']
];
if(empty($fields['name'])){
$errors['name'] = 'Come on you have a name right?';
}
if(empty($fields['email'])){
$errors['email'] = 'Yes right, we need your email so that we contact you';
}
if(empty($fields['number'])){
$errors['number'] = 'We also need your number';
}
if(empty($fields['message'])){
$errors['message'] = 'You have come this far for sure you have something to say right';
}
if(count($errors) > 0){
if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ){
echo json_encode($errors);
exit;
}
echo "<ul>";
foreach($errors as $key => $value){
echo '<li>' . $value . '</li>';
}
echo '</ul>';
exit;
}
/*foreach($fields as $field => $data){
if(empty($data)){
$errors[] = 'The ' . $field . ' field is required.';
}
}*/
if(empty($errors)){
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->Host = 'smtp.gmail.com';
$m->Username = '';
$m->Password = '';
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->isHTML();
$m->Subject = 'Contact form submitted';
$m->Body = 'From: ' . $fields['name'] . '(' .$fields['email'] . ')<p>' . $fields['message'] . '</p><p>' . $fields['number'] . '</p>';
$m->FromName = 'contact';
$m->AddReplyTo($fields['email'], $fields['name']);
$m->AddAddress('');
if($m->send()){
header('Location: ../contact.php');
die();
} else {
$errors[] = 'Sorry could not send email. Try again later.';
}
}
} else {
echo 'Oops something went wrong';
}
$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;
header('Location: ../contact.php');
It's hard to find out what is what in your code, but i guess this should work with the changes i've made.
jQuery / ajax
success: function(resp){
if(resp === 'Success'){
console.log('Success');
$('form').submit();
return false;
} else {
$.each(resp, function(i, v){
console.log(i + " => " + v);
var msg = '<label class="error" for"' +i+ '">' +v+ '</label>';
$('input[name="' +i+ '"], textarea[name="' +i+ '"], select[name="' +i+ '"]').addClass('inputTextError').after(msg);
});
var keys = Object.keys(resp);
$('input[name="' +keys[0]+ '"]').focus
}
return false;
}
PHP
<?php
if(isset($_POST['name'], $_POST['email'], $_POST['number'], $_POST['message'])){
$errors = array();
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
'number' => $_POST['number'],
'message' => $_POST['message']
];
if(empty($fields['name'])){
$errors['name'] = 'Come on you have a name right?';
}
if(empty($fields['email'])){
$errors['email'] = 'Yes right, we need your email so that we contact you';
}
if(empty($fields['number'])){
$errors['number'] = 'We also need your number';
}
if(empty($fields['message'])){
$errors['message'] = 'You have come this far for sure you have something to say right';
}
if(count($errors) > 0){
if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ){
echo json_encode($errors);
exit;
}
echo "<ul>";
foreach($errors as $key => $value){
echo '<li>' . $value . '</li>';
}
echo '</ul>';
exit;
}
/*foreach($fields as $field => $data){
if(empty($data)){
$errors[] = 'The ' . $field . ' field is required.';
}
}*/
if(empty($errors)){
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->Host = 'smtp.gmail.com';
$m->Username = '';
$m->Password = '';
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->isHTML();
$m->Subject = 'Contact form submitted';
$m->Body = 'From: ' . $fields['name'] . '(' .$fields['email'] . ')<p>' . $fields['message'] . '</p><p>' . $fields['number'] . '</p>';
$m->FromName = 'contact';
$m->AddReplyTo($fields['email'], $fields['name']);
$m->AddAddress('hello@jilstudios.com', 'JIL Studios');
if($m->send()){
echo 'Success';
} else {
$errors[] = 'Sorry could not send email. Try again later.';
}
}
} else {
echo 'Oops something went wrong';
}
$_SESSION['errors'] = $errors; $_SESSION['fields'] = $fields;
header('Location: ../contact.php');
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.