javascript - How can I display a success message after the user submit the form -
$('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 problem checking fields'); } }); return false; }); }); function reseterrors(){ $('form input, form textarea').removeclass('inputtexterror'); $('label.error').remove(); }
here's code have tried still ain't working plz i'm doing validation on server i'm using ajax display errors display success message it's little bit confusing
here's 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 have name right?'; } if(empty($fields['email'])){ $errors['email'] = 'yes right, need email contact you'; } if(empty($fields['number'])){ $errors['number'] = 'we need number'; } if(empty($fields['message'])){ $errors['message'] = 'you have come far sure have 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 $key => $value){ echo '<li>' . $value . '</li>'; } echo '</ul>'; exit; } /*foreach($fields $field => $data){ if(empty($data)){ $errors[] = 'the ' . $field . ' field 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 not send email. try again later.'; } } } else { echo 'oops went wrong'; } $_session['errors'] = $errors; $_session['fields'] = $fields; header('location: ../contact.php');
it's hard find out what in code, guess should work 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 have name right?'; } if(empty($fields['email'])){ $errors['email'] = 'yes right, need email contact you'; } if(empty($fields['number'])){ $errors['number'] = 'we need number'; } if(empty($fields['message'])){ $errors['message'] = 'you have come far sure have 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 $key => $value){ echo '<li>' . $value . '</li>'; } echo '</ul>'; exit; } /*foreach($fields $field => $data){ if(empty($data)){ $errors[] = 'the ' . $field . ' field 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 not send email. try again later.'; } } } else { echo 'oops went wrong'; } $_session['errors'] = $errors; $_session['fields'] = $fields; header('location: ../contact.php'); ?>
Comments
Post a Comment