php - display email variable in body of message

Go To StackoverFlow.com

0

I am setting up an opt-in list for a newsletter. Below is my php script, which is working, but when I receive the email, I can't see the email address in the message, only this:

Email:

How can I get the email variable to display in the email message?

Edit: I'm posting the form using jquery/ajax, that's why there's no post and action in the form.

PHP (newsletter.php)

<?php 

$to = "newsletter@example.com";
$subject = "Send me the newsletter";

$email = $_POST['email'];

$email = filter_var($email , FILTER_SANITIZE_EMAIL);

$message = "Email: $email";

mail($to, $subject, $message, "From: no-reply@example.com");

?>

HTML

<form id="newsletter_signup">
<input type="text" name="email" id="email" placeholder="Email Address" /><input type="submit" value="Submit" class="submit_button" />
</form>

jQuery

$.ajax({
          type: "POST",
          url: "newsletter.php",
          data: $("input#email").val(),
          success: function() {
        $('#signup').html("<div id='message'></div>");
        $('#message').html("<h3>Blah, blah, blah!</h3>")
        .css({color:"#00000", fontFamily: "Arial", fontSize: "12px"})
        .hide()
      }
     });
2012-04-03 23:24
by chowwy
Did you make sure that $POST['email'] was valid and set? How can you expect a $POST variable when you haven't told the form to POST to your server. Form methods are GET by default - Blake 2012-04-03 23:27
I'm using jquery/ajax to post the form. My question has been updated to include the ajax info - chowwy 2012-04-04 01:34


1

Try this:

$.ajax({
          type: "POST",
          url: "newsletter.php",
          data: {"email" : $("input#email").val()},
          success: function() {
        $('#signup').html("<div id='message'></div>");
        $('#message').html("<h3>Blah, blah, blah!</h3>")
        .css({color:"#00000", fontFamily: "Arial", fontSize: "12px"})
        .hide()
      }
     });

Also check out jQuery serialize method (you can just type data: $("form").serialize()). http://api.jquery.com/serialize/

2012-04-04 01:49
by Daniil Ryzhkov
Much appreciated. Accepted and upvoted - chowwy 2012-04-04 02:05


0

Your from should be

<form id="newlsetter_signup" method="POST" action="">
2012-04-03 23:27
by Darrell Ding