I have a small form, once submitted the post data gets set to variables which are then used in a email message, when the email is sent, the post data is blank, when I try to echo out the POST data it is also blank. I am not sure why, here is the code.
<?php if(!isset($_POST['submit'])): ?>
<form name="contactForm" method="POST" id="contactForm">
<table>
<tr>
<td><input name="name" id="name" class="fields" type="text" placeholder="name"></td>
</tr>
<tr>
<td><input name="email" class="fields" type="email" placeholder="email"></td>
</tr>
<tr>
<td><input name="phone" class="fields" type="phone" placeholder="phone(optional)"></td>
</tr>
<tr>
<td><textarea name="message" class="fieldsMessage" name="message" placeholder="Message"></textarea><?php $ermes; if(!empty($ermes)){echo "<br>".$ermes;}?></td>
</tr>
<tr>
<td><input class="submitButton" type="submit" name="submit" value="Submit">
</td>
</tr>
</table>
</form>
<?php else: ?>
<h2 style="padding-bottom: 80px; margin-bottom: 0px; text-align: center;" class="title">Your Message Was Sent</h2>
<?php
if(isset($_POST['submit'])){
if(empty($_POST['name'])){
echo "<script type=text/javascript>alert('PLEASE ENTER YOUR NAME');</script>";
} //end name if
elseif(empty ($_POST['email'])){
echo "<script type=text/javascript>alert('PLEASE ENTER YOUR EMAIL');</script>";
}//end else if email
elseif(empty($_POST['message']))
{
echo "<script type=text/javascript>alert('Please enter a message');</script>";
}// End elseif message
}//End isset submit if
else
{
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
if(isset($_POST['phone']))
{
$phone=$_POST['phone'];
}// isset phone
else
{
$phone="";
}
$to="sample@live.com";
$subject="New Contact Form Email";
$message="Name: ".$_POST['name'];
$headers='From: Trip Galapagos Forms';
mail($to, $subject, $message);
}//End else
?>
<?php endif;?>
Where is the action in your form?
The action attribute specifies where to send the form-data when a form is submitted.
Please refer html form tag for more information http://www.freecontactform.com/html_form.php#htmlform
Also check here and HTML_forms_-_the_basics
You miss to close the if statement endif;
in end of code.
If you didn't specify the action
in form
. Data will be posted in same location.
The action
for form
is missing. When you are posting data to the same page you have to use action="<?php echo $_SERVER['PHP_SELF']?>"
or action=""
in the form tag.
Change
<form name="contactForm" method="POST" id="contactForm">
to
<form name="contactForm" method="post" action="" id="contactForm">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.