<?php $data= array(); $from = $_POST['email']; $full_name = $_POST['fullname']; $number=$_POST['number']; $subject = "Contact Form submission"; $messages = $full_name . " " . " sent you this message:" . "\n\n" . $_POST['message']. "\nPlease contact ". $full_name ." via email ". $from . " or call them at ". $number; $data['success'] = true; $data['message'] = 'Thank you. We will get back to you shortly!'; // return all our data to an AJAX call echo json_encode($data); ?>
$(document).ready(function() { // process the form $('form').submit(function(event) { // get the form data // there are many ways to get this data using jQuery (you can use the class or id also) var formData = { 'email' : $('input[name=email]').val(), 'fullname' : $('input[name=fullname]').val(), 'number' : $('input[name=number]').val(), 'message' : $('input[name=message]').val() }; // process the form $.ajax({ type : 'POST', // define the type of HTTP verb we want to use (POST for our form) url : 'mail_processor.php', // the url where we want to POST data : formData, // our data object dataType : 'json', // what type of data do we expect back from the server encode : true }) // using the done promise callback to handle successful ajax request .done(function(data) { // log data to the console so we can see console.log(data); $('form').append('<div class="alert alert-success">' + data.message + '</div>'); // usually after form submission, you'll want to redirect // window.location = '/thank-you'; // redirect a user to another page alert('success'); // for now we'll just alert the user }); // stop the form from submitting the normal way and refreshing the page event.preventDefault(); }); });
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)