Hi please excuse the rookie question as I am still very much learning PHP. Im trying to do a basic form validation. Im trying to get any errors found upon validation to be displayed in red inside the form field. If no errors are found the form should redirect to a Thank you page.
Ive written a basic validation, but I am now stuck on how to correctly implement it.
My question is:
- How do I get the error messages to display inside the form field WITHOUT sending the form?
- How do I procees to send the form if no errors is found?
My code follows:
HTML
<form id="enquire" method="post" action="thankyou.php">
<h2>Test Drive an Audi Today</h2>
<input type="text" value="Name" class="textbox" id="name" name="name" onfocus="if(this.value=='Name') this.value='';" /><br />
<br />
<input type="text" value="Surname" class="textbox" id="surname" name="surname" onfocus="if(this.value=='Surname') this.value='';" /><br />
<br />
<input type="text" value="Contact Number" class="textbox" id="nr" name="nr" onfocus="if(this.value=='Contact Number') this.value='';" /> <br />
<br />
<input type="text" value="Email" class="textbox" id="email" name="email"onfocus="if(this.value=='Email') this.value='';" /><br />
<br />
<select id="swapImg" name="model" class="modelSelect" onchange="swapImage()" >
<option value="images/1.jpg">A4</option>
<option value="images/2.jpg" selected="selected">A6</option>
<option value="images/3.jpg">A8</option>
</select>
<br />
<br />
<input type="submit" name="submit" class="butt" value="Send" />
<span class="buttonText">We'll Call you Back!</span>
</form>
PHP
if (isset($_POST['enquire'])){
//condition
$correct = false;
//Get Info
$name = $_POST['name'];
$surname = $_POST['surname'];
$number = $_POST['nr'];
$email = $_POST['email'];
$_REQUEST['model'];
//name fields
$string_exp = "/^[A-Za-z .'-]+$/";
//email field
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
//validate name field
if($name == ""){
$name_error="* Please enter your name";
$correct = false;
}
else if (!preg_match($string_exp,$name)) {
$name_error= "* Wrong Format";
$correct= false;
}
//validate surname
if($surname == ""){
$surname_error= "* Please enter your surname.";
$correct= false;
}
else if(!preg_match($string_exp,$surname)){
$surname_error= "* Wrong Format";
$correct = false;
}
//check email
if($email==""){
$email_error="* Please enter your email";
$correct = false;
}
else if(!preg_match($email_exp,$email)){
$email_error = "* Wrong Format";
$correct = false;
}
//check number
if($number=""){
$phone_error = "* Please enter your phone number";
$correct = false;
}
else if(!is_numeric($number)){
$phone_error ='* Please enter only numbers 0-9.';
$correct = false;
}
//proceed if all fields validate
if($correct){
$email_to = "[email protected]";
$subject ="New Enquiry";
//set message
$email_message .= "First Name: ".($name)."\n";
$email_message .= "Last Name: ".($lastname)."\n";
$email_message .= "Email: ".($email)."\n";
$email_message .= "Telephone: ".($telephone)."\n";
$email_message .= "Model: ".($model)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
@mail($email, $email_subject, $email_message, $headers);
header("thankyou.php");
exit();
}//end if
}// end isset
if anyone could point me in the right direction it would be greatly appreciated.