I know this is only a simple post to a php file through Ajax. It is something I have done before, but there must be something I am missing this time. I cannot figure out why my PHP file wont read or echo back any of the posted data. The PHP code works fine when it is in the same file as the form, but when I move the PHP file to an external source, it ceases to work. All the data shows up in header, but it is not being read.
Request Payload
name=Form+Name&email=myemail%40email.com&tel=2345557777&web=http%3A%2F%2Fmywebsite.com&msg=This+is+the+message Name
Form data (the call back doesnt return any data)
contact.on('submit', function(){
var contactData = contact.serialize();
console.log(contactData);
return ajaxPost('mail.php', 'POST', contactData, (data) => {
console.log(data);
// Contact form callback
alert('Thanks for contacting us!');
});
});
Ajax Post
var ajaxPost = function (x, y, z, callback){
$.ajax({
url: x,
type: y,
data: z,
// encode: true,
processData: true,
contentType: false,
dataType: 'html'
}).done(() => {
callback();
});
event.preventDefault();
};
PHP File
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$msg = $_POST['msg'];
$web = $_POST['web'];
$admin_email = "myemail@email.com";
mail($admin_email, 'Name: ' . $name . " Email: " . $email, ' '. "Message: " . $msg . " Website: " . $web);
echo $name . $email . $tel;
HTML form
<div class="form col-lg-6 col-md-6 col-sm-6 col-xs-12 text-center animated slideInDown">
<form id="contact" name="contact-form" action="" method="post" datatype="multipart/form-data">
<h3 class="text-center">Drop us a line</h3>
<fieldset>
<input placeholder="Your name" name="name" type="text" tabindex="1" required autofocus>
</fieldset>
<fieldset>
<input placeholder="Your Email Address" name="email" type="text" tabindex="2" required>
</fieldset>
<fieldset>
<input placeholder="Your Phone Number (optional)" name="tel" type="text" tabindex="3" required>
</fieldset>
<fieldset>
<input placeholder="Your Web Site (optional)" name="web" type="text" tabindex="4">
</fieldset>
<fieldset>
<textarea placeholder="Type your message here...." name="msg" tabindex="5" required></textarea>
</fieldset>
<fieldset>
<button type="submit" id="contact-submit">Submit</button>
</fieldset>
<a id='number-attn' class="glyphicon glyphicon-phone" href="tel:5555555555"> 5555555555</a>
</form>
</div>
</div>