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()
}
});
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/
Your from should be
<form id="newlsetter_signup" method="POST" action="">