Reputation: 75
I'm having trouble getting the php script correct for my contact form. I managed to have it email me but it doesn't show a name, email or text in the email.
PHP Script
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'myemail@email.comm';
$subject = 'Hello';
mail ($to, $subject, $message, "From: " . $name);
echo "Your Message has been sent.";
?>
Contact Form
<form role="form" action="contact.php">
<div class="form-group">
<label for="InputName">Name</label>
<input name="name" type="text" class="form-control" id="InputName" placeholder="Enter Name">
</div>
<div class="form-group">
<label for="InputEmail">Email Address</label>
<input name="email" type="email" class="form-control" id="inputEmail" placeholder="Enter email">
</div>
<div class="form-group">
<label for="InputText">Message</label>
<input name="message" type="text" class="form-control" id="InputText" placeholder="Enter Text">
</div>
<button name="submit" type="submit" class="btn btn-default">Send</button>
</form>
Upvotes: 3
Views: 409
Reputation: 74220
You're using POST variables, yet your form being the following:
<form role="form" action="contact.php">
doesn't have a POST method defined. Form defaults to GET when a method isn't defined.
Therefore, you will need to change your form to
<form role="form" action="contact.php" method="post">
From a comment you left:
"I'm still having a problem where instead of giving me the email they input it gives an email created by the hosting company which is no good."
A: It's most likely because of how you're using From:
in your code, which is the person's name. Mail is expecting an Email address.
Replace:
mail ($to, $subject, $message, "From: " . $name);
with:
mail ($to, $subject, $message, $header);
and adding the following under $subject = 'Hello';
$header = "From: ". $name . " <" . $email . ">\r\n";
That way, you will see the person's name in the Email, while having a valid "From" header.
Additional notes:
I also suggest that you test if any of the fields aren't left empty using:
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']))
{
// execute code
}
Otherwise, anyone could send an Email without any information at all.
You can also add an else{}
to it like else { echo "Please fill in all the fields."; }
Upvotes: 9
Reputation: 747
You should specify POST method:
<form role="form" method="POST" action="contact.php">
Upvotes: 1
Reputation: 18891
Your form needs the attribute
method="POST"
Without this, the browser defaults to method="GET"
, which submits the form in the url, e.g., http://example.com/default.php?name=First%20Last&email=...
Upvotes: 1