Reputation: 854
I'm building a site in wordpress and usually use the contactform7 plugin to handle forms, but am trying to learn how to set up a form without having to rely on the plugin. I've had success with this recently, but on this latest site my content is being sent to the recipient. I get an email, but none of the values from the form are being sent.
Here's my form (I know I have to also learn about validation/ sanitization, that will be another topic!):
<form action="<?php echo home_url('/'); ?>sent/" method="POST" class="col-sm-7">
<input id="name" placeholder="name:" name="name" type="text" class="form-control" required></label>
<input id="email" placeholder="email:" name="email" type="text" class="form-control" required>
<textarea id="message" placeholder="message:" name="message" class="form-control" rows="8" required></textarea>
<button id="contact-submit" type="submit" class="btn form-control">Submit</button>
</form>
When the user presses submit the page redirects to url/sent/ in that file I have this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name, $email, \nMessage: $message";
$recipient = "[email protected]";
$subject = "Contact Form Submission";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
Now when I get the email in my inbox it shows up like this:
From: , , Message:
That's it. Is there something obvious I'm missing here? Any help is, as always, greatly appreciated.
thanks!
Added note: It seems that when the submit button is pressed and the page redirects, it's pulling a 404, though the url it's redirecting to IS correct, in fact, even if I just refresh the page without changing the url it drops the 404 and loads the page content. I guess this is why the content is not being sent, but then why is this happening?
Upvotes: 0
Views: 108
Reputation: 854
As it turns out this problem is due to an issue with wordpress. Wordpress does not allow you to use the name="name" - this causes a 404 and the form values are not sent. I changes the name to 'form_name' and it works perfectly now.
Thanks for your help, hope this can help someone else too!
Thanks to Tom Elliot at http://www.webdevdoor.com for the help! http://www.webdevdoor.com/wordpress/submitting-form-wordpress-redirects-404-page/
Upvotes: 1
Reputation: 45490
You should always check to see if the values are set
if(isset($_POST['name']) && isset($_POST['email'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name, $email, \nMessage: $message";
$recipient = "[email protected]";
$subject = "Contact Form Submission";
$mailheader = "From: $email \r\n";
if(mail($recipient, $subject, $formcontent, $mailheader)){
echo "message sent!";
}else{
die("Error!");
}
}else{
echo "email or name is not set";
}
Also try to change your button to <input type="submit">
even though it might not make a difference.
Other than that the code looks fine
Upvotes: 0