4

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 nature of how emails work, and suggested to use PHP (combined with AJAX) to handle the server-side code. I followed the guide here but I am not receiving any emails at my email address. I am testing locally on my machine (using XAMPP) before I deploy my code on my client's web space (goDaddy). I want to note that I have never used PHP before.

Javascript:

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

PHP (email.php):

<?php
$to = "[email protected]";
$subject = "This is my email";
$message = $_REQUEST;
$send = mail($to, $subject, $message);
if(!$send){    
    die();  
}
?>
4
  • Do you have a mail delivery mechanism on your local machine which can be used by php? Commented Jul 1, 2012 at 15:53
  • BTW The message will only print the text Array, because $_REQUEST is an array. Commented Jul 1, 2012 at 15:56
  • You might also need to setup your email settings in XAMPP. Check here: stackoverflow.com/questions/4652566/php-mail-setup-in-xampp Commented Jul 1, 2012 at 15:57
  • Oh, I didn't realize you needed a mail delivery mechanism. I will install that now. What about when I deploy my code onto my client's webhost (goDaddy)? Will I need to do anything special? Commented Jul 1, 2012 at 16:03

5 Answers 5

2

XAMPP doesn't have an e-mail sending thing by itself as default. You may check the links below;

php mail setup in xampp

How do I enable XAMPP to locally use the php's mail() function so I can test my mail() scripts locally without having to upload to my server?

Sign up to request clarification or add additional context in comments.

Comments

2

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

9 Comments

Although you are correct I doubt it will solve the issue of OP.
@PeeHaa Why? Give Uhehesh a chance to improve his/her answer.
I think my answer won't really help him with e-mail issue but will save his time thinking about why only "Array" is sent.
@Uhehesh Ah, I did not realize that. That would have saved me quite a lot of time. Thank you :)
Also if you use $_REQUEST["data"] then you need data: {data: data} in ajax call.
|
1

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="[email protected], [email protected]">
<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';
}
?>

Comments

0
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']

Comments

0

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;
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.