Mình đang làm trang liên hệ và muốn sử dụng Ajax form, bác nào biết chỉ mình với nhé, mình viết code bằng php. Có ví dụ cụ thể kết hợp Ajax với php hộ mình với nha. Thanks
Chia sẻ
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Hoàng Anh
Một ví dụ mình cũng đang làm tương tự nhé
HTML:
Input your name
JS:
$('#contactform').submit(function () {
var action = $(this).attr('action');
var form = $(this);
$("#message-contact").slideUp(750, function () {
$('#message-contact').hide();
$('#submit-contact')
.after('')
.attr('disabled', 'disabled');
$.ajax({
type: "POST",
url: action,
data: form.serialize(),
success: function(data)
{
$('#message-contact').slideDown('slow');
$('#contactform .loader').fadeOut('slow', function () {
$(this).remove()
});
$('#submit-contact').removeAttr('disabled');
if(data.success==1)
{
document.getElementById('message-contact').innerHTML = data.message;
$('#contactform').slideUp('slow');
} else {
var key_id = Object.keys(data.errors)[0];
$('#'+key_id).addClass('is-invalid').focus();
document.getElementById('message-contact').innerHTML = data.errors[key_id];
}
}
});
});
return false;
});
PHP:
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
$name_contact = $_POST['name_contact'];
$lastname_contact = $_POST['lastname_contact'];
$email_contact = $_POST['email_contact'];
$phone_contact = $_POST['phone_contact'];
$message_contact = $_POST['message_contact'];
$verify_contact = $_POST['verify_contact'];
if(trim($name_contact) == '') {
$errors['name_contact'] = 'You must enter your Name.';
} else if(trim($lastname_contact ) == '') {
$errors['lastname_contact'] = 'You must enter your Last name.';
} else if(trim($email_contact) == '') {
$errors['lastname_contact'] = 'Please enter a valid email address.';
exit();
} else if(!isEmail($email_contact)) {
$errors['lastname_contact'] = 'You have enter an invalid e-mail address, try again.';
} else if(trim($phone_contact) == '') {
$errors['lastname_contact'] = 'Please enter a valid phone number.';
} else if(!is_numeric($phone_contact)) {
$errors['lastname_contact'] = 'Phone number can only contain numbers.';
} else if(trim($message_contact) == '') {
$errors['lastname_contact'] = 'Please enter your message.';
} else if(!isset($verify_contact) || trim($verify_contact) == '') {
$errors['lastname_contact'] = ' Please enter the verification number.';
} else if(trim($verify_contact) != '4') {
$errors['lastname_contact'] = 'The verification number you entered is incorrect.';
}
// 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'] = 0;
$data['errors'] = $errors;
} else {
// if there are no errors process our form, then return a message
// DO ALL YOUR FORM PROCESSING HERE
// THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)
// show a message of success and provide a true success variable
$data['success'] = 1;
$data['message'] = " Email Sent.
Thank you $name_contact,
your message has been submitted. We will contact you shortly.";
}
echo json_encode($data);