This page works and the form email is sent.
The required fields are fname,lname,company and email.
However, if they leave off one of the required fields it goes to the "Thank you for submitting " second half of the if/else.
How can I get them to stay in the form until all the required fields are entered?
Do I need to use javascript or php but I need them to stay inside the if.
I didn't use the
tags because I am using <?php ?> as enclosing brackets around the html.
I tried using echo to echo out the html but I am fixing this page and it is a jumble of nested tables, css, html and javascript. using echo just got so confusing I gave up.THANKS,
<?php
if(isset($_POST['email'])){
$email_to = "janis.rough@gmail.com";
$email_subject = "Distributor_Inquiry";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br /> ";
echo "Please go back and fix these errors.<br /><br />";
}
if(!isset($_POST['first']) ||
!isset($_POST['last']) ||
!isset($_POST['company']) ||
!isset($_POST['email'])){
died('We are sorry, but there appears to be a problem with the form you submitted. ');
}
$first_name = $_POST['first']; // required
$last_name = $_POST['last']; // required
$company = $_POST['company']; // required
$address = $_POST['address']; // not required
$address2 = $_POST['address2']; //not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Company: ".clean_string($company)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "Address2: ".clean_string($address2)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml">
<BODY>
<h1 class="leftpad">Become a Distributor</h1>
<!-- </div>-->
<div class="main col1-layout">
<div class="col-main">
<div class="category-view">
<div id="dist-wrap" style="margin-left:40px;margin-top:10px;">
<div class="how-to-order" class="dist" >
<h2>Submit this form if you wish your company to be considered by our Distribution Sales Manager.</h2>
<form name="distributor_inquiry_form" action="test_distributor_form.php" method="post" >
<table width="800px" align="center">
<tr>
<td width="30%" align="right"><strong>Name:</strong></td>
<td width="35%" ><input name="first" size="30" type="text" placeholder="First Name" class="input" /></td>
<td width="35%"><input name="last" size="30" class="input" type="text" placeholder="Last Name" /></td>
</tr>
<tr>
<td align="right" width="30%"><strong>Company:</strong></td>
<td width="70%" colspan="2"><input name="company" class="input2" type="text" placeholder="Company" size='30' /></td>
</tr>
<tr>
<td align="right" width="30%"><strong>Address:</strong></td>
<td width="35%">
<input size="30" size='30' name="address" class="input" type="text" placeholder="Address" /> </td>
<td width='35%'><input size='30' name="address2" class='input' type="text" placeholder="Address 2" /></td>
</tr>
<tr>
<td align='right' width='30%'><strong> City/State or Province:</strong></td>
<td width='35%'> <input size='30' class='input' name="city" type="text" placeholder="City" /> </td>
<td width='35%'>
<select name='state' class='input' width='35%' >
<option value='' selected='selected'>Select a State </option>
<option VALUE="UNK">Outside US / Canada</option>
<option VALUE="AL">Alabama</option>
<option VALUE="AK">Alaska</option>
</select>
</td></tr>
<tr>
<td align='right' width='30%'><strong>Zip Code:</strong></td> <td width='70%' colspan="2">
<input size='9' class="input2" name="zipcode" type="text" placeholder="Zip Code"/></td>
</tr>
<tr>
<td align='right' width='30%'><strong>Country:</strong></td>
<td width='70%'colspan="2"><input size='30' class="input2" name="country" cols='100' placeholder="Country" /></td>
</tr>
<tr>
<td width='30%' align="right"><strong> Phone/Email</strong></td><td width='35%'>
<input size='12' name="phone" type="text" class='input' placeholder="Phone" /></td>
<td align='right' width='35%'><input size='30' name="email" class='input' placeholder="Email" type="text" /></td>
</tr>
<tr>
<td align='right' width='30%' ><strong>Distributor Web Site</strong></td><td colspan="2" width='70%'>
<input size='30' class="input2" name="url" type="text" placeholder="Distributor Web Site" /></td>
</tr>
<tr>
<td align='right' width='30%' ><strong>Message:</strong></td><td width='70%' height='25%' colspan="2">
<textarea rows="5" name="message" cols="115" ></textarea></td>
</tr>
<tr>
<td height="24"></td>
<td><input id='submit' type="submit" name="submit" value="SEND" /></td>
</tr>
</table>
</form>