Hi I am working on a contact us form. I got the form working correctly, but when it validates it puts the error on a new page. How can I have the error message show on the form page?
HTML Code:
<form action="contact.php" method="post" id="contactform">
<ol>
<li>
<label for="name">Full Name <span class="red">*</span></label>
<input id="name" name="name" class="text" />
</li>
<li>
<label for="email">Your email <span class="red">*</span></label>
<input id="email" name="email" class="text" />
</li>
<li>
<label for="phone">Phone Number <span class="red">*</span></label>
<input id="phone" name="phone" class="text" />
</li>
<li>
<label for="company">Company</label>
<input id="company" name="company" class="text" />
</li>
<li>
<label for="topic">Subject<span class="red">*</span></label>
<input id="topic" name="topic" class="text" />
</li>
<li>
<label for="comments">Message <span class="red">*</span></label>
<textarea id="comments" name="comments" rows="6" cols="50"></textarea>
</li>
<li class="buttons">
<input type="image" name="imageField" id="imageField" src="images/send.gif" />
</li>
</ol>
</form>
The PHP code:
<?php
// Pick up the form data and assign it to variables
$name = check_input ($_POST['name'], "Please enter your name");
$email = check_input ($_POST['email'],"Please enter your email");
$phone = check_input ($_POST['phone'], "Please enter your phone number");
$company = $_POST['company'];
$topic = check_input ($_POST['topic'], "Please enter your subject");
$comments = check_input ($_POST['comments'], "Please enter your message");
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if (strlen($data) == 0)
{
die($problem);
} else {
return $data;
}
}
// Build the email (replace the address in the $to section with your own)
$to = 'email@email.com"';
$subject = "New message: $topic";
$message = "Email: $email \n Phone: $phone \n Company: $company \n $name said: $comments";
$headers = "From: $email";
// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);
// Redirect
header("Location: success.html");
?>