Skip to content Skip to sidebar Skip to footer

Send Email With PHP/AJAX From HTML Form

I want to send the information the user has filled out from an HTML form to my email address. From my understanding, this cannot be done by using only client-side coding due to the

Solution 1:


Solution 2:

You should replace $_REQUEST with $_REQUEST["data"] or something like this, because $_REQUEST is an array.


Solution 3:

all time the page is being refreshed. Below is my code:

HTML 5:

<form method="post" data-ajax="false">
<input type="text" name="email" placeholder="jack@example.com, jil@example.com">
<input type="submit" name="submit" value="Invite" class="submitBtn"> 
</form>

JS:

$('form').bind('submit',function(){
$.ajax({
type    : 'POST',
url     : 'data/invite.php',
data    : $(this).serialize(),
cache   : false,
dataType: 'text',
success : function (serverResponse) { alert('mail sent successfully.'); },
error   : function (jqXHR, textStatus, errorThrown) {alert('error sending mail');}
});
})

PHP:

<?php
$to         = $_POST['mail'];
$name       = 'Indusnet Technologies';
$subject    = 'Think Recycle';
$text       = 'Welcome to our site';

$message =' You received  a mail from '.$name;
$message .='Text of the message : '.$text;

if(mail($to, $subject,$message)){
    echo 'mail successful send';
}
else{
    echo 'there’s some errors to send the mail, verify your server options';
}
?>

Solution 4:

var data = "This is my email";
$.ajax({
    type: "POST",
    url: "email.php",
    data: data,
    dataType: "text"
});

This Code shoud be like this

var data = "This is my email";
$.ajax({
    type: "POST",
    url: "email.php",
    data: {"data": data},
    dataType: "text"
});

and get in code either through $_POST['data'] or $_REQUEST['data']


Solution 5:

You probably should return false to the form. Try this:

$('form').submit(function(){
  $.ajax({
    type    : 'POST',
    url     : 'data/invite.php',
    data    : $(this).serialize(),
    cache   : false,
    dataType: 'text',
    success : function (serverResponse) { alert('mail sent successfully.'); },
    error   : function (jqXHR, textStatus, errorThrown) {alert('error sending mail');}
  });

return false;
})

Post a Comment for "Send Email With PHP/AJAX From HTML Form"