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 = "myself@hotmail.com";
$subject = "This is my email";
$message = $_REQUEST;
$send = mail($to, $subject, $message);
if(!$send){    
    die();  
}
?>
Community
  • 1
  • 1
Jon
  • 2,249
  • 6
  • 26
  • 30
  • Do you have a mail delivery mechanism on your local machine which can be used by php? – PeeHaa Jul 01 '12 at 15:53
  • BTW The message will only print the text `Array`, because `$_REQUEST` is an array. – PeeHaa Jul 01 '12 at 15:56
  • You might also need to setup your email settings in XAMPP. Check here: http://stackoverflow.com/questions/4652566/php-mail-setup-in-xampp – septemberbrain Jul 01 '12 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? – Jon Jul 01 '12 at 16:03

5 Answers5

2

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

Uhehesh
  • 506
  • 2
  • 8
  • 22
  • Although you are correct I doubt it will solve the issue of OP. – PeeHaa Jul 01 '12 at 15:57
  • @PeeHaa Why? Give Uhehesh a chance to improve his/her answer. – 11684 Jul 01 '12 at 16:01
  • 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 Jul 01 '12 at 16:02
  • 1
    @Uhehesh Ah, I did not realize that. That would have saved me quite a lot of time. Thank you :) – Jon Jul 01 '12 at 16:03
  • Also if you use `$_REQUEST["data"]` then you need `data: {data: data}` in ajax call. – jcubic Jul 01 '12 at 16:05
  • @Jon Did it work? In that case, Give Uhehesh a nice green root sign. – 11684 Jul 01 '12 at 16:06
  • @11684 I'm not quite sure yet. I'm still trying to set-up the Mercury. I am new to XAMPP so I'm watching a video tutorial as I didn't understand the tutorials on stackoverflow. – Jon Jul 01 '12 at 16:12
  • @Jon Okay. It's just that I thought you said "Thank you, it's working" in your previous comment and I sometimes forget to accept answers. – 11684 Jul 01 '12 at 16:15
  • @11684 My apologies. I'm just new to XAMPP and PHP. By any chance do you know if I will need to set up some sort of mailing mechanism on my client's webhost (goDaddy)? – Jon Jul 01 '12 at 16:21
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?

Community
  • 1
  • 1
Mustafa
  • 825
  • 3
  • 14
  • 37
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="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';
}
?>
Bluerain
  • 477
  • 5
  • 9
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']

Mudaser Ali
  • 3,989
  • 3
  • 25
  • 27
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;
})
Kamil
  • 1