I am trying to send email using jquery and PHP but somehow I am not receiving emails even when PHP executes successfully.
This is my javascript code:
<script type='text/javascript'>
$("#formoid").submit(function(event) {
event.preventDefault();
var $form = $(this),
url = $form.attr('action');
var posting = $.post(url, {
name: $('#name').val(),
email: $('#email').val(),
message: $('#message').val()
});
posting.done(function(data) {
$("#result").empty().append('Thank you for contacting. Will get back to you as sson as possible.');
});
});
</script>
My PHP looks like this:
<?php
if(isset($_POST['submit'])){
$to = "[email protected]"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$email = $_POST['email'];
$subject = "Portfolio form submission";
$message = $name . " " . $email . " wrote the following:" . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
}
?>
When I submit the form, and inspect the dom, I see the results are good. In the XHR details, it shows:
statusText: "OK"
Am i doing something wrong here?
Edit: Adding HTML as requested:
<form class="no-bottom" id="formoid" action="contact.php" title="" method="post">
<label for="name" class="sr-only">Name</label>
<input type="text" class="underline" name="name" id="name" placeholder="Your name..." required>
<label for="email" class="sr-only">Email</label>
<input type="email" class="underline" name="email" id="email" placeholder="Your email..." required>
<label for="message" class="sr-only">Message</label>
<textarea class="underline" name="message" id="message" cols="30" rows="6" placeholder="Here goes the message..." required></textarea>
<div class="margin-1"></div>
<input type="submit" value="Send it">
</form>
isset($_POST['submit']) is checking if the key submit is present in the date Posted from the client.
However, the data sent from client does not include it.
var posting = $.post(url, {
name: $('#name').val(),
email: $('#email').val(),
message: $('#message').val()
});
To make it work, you can either add it in POST data or check another key in the server.
var posting = $.post(url, {
name: $('#name').val(),
email: $('#email').val(),
message: $('#message').val(),
submit: 'anythingTruthyHere' // Added `submit` key
});
OR, you can check if the email is present in the POST data
if (isset($_POST['email'])) {
// send mail code here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With