値をrequired=配列に追加し、すべてが必要なわけではない場合を除いて、フォームに値が表示されません。したがって、firname、lasname、およびemailは送信された電子メールに表示されますが、電話とメッセージは表示されません(他のフィールドも追加できます)。
これが私のコードです:
<?php
if(!$_POST) exit;
$email = $_POST['email'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array('firname','lasname','email','phone','message');
$required = array('firname','lasname','email',);
$your_email = "aasdasd@email.com";
$email_subject = "Reservations Form: ".$_POST['subject'];
$email_content = "new reservation:\n";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != '') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
if(@mail($your_email,$email_subject,$email_content)) {
header('Location: http://www.example.com/test/reservation-submission.html');
} else {
echo 'ERROR!';
}
}
?>`
<form action="reservations.php" method="post" id="contactform">
<ol>
<li><label>First Name *</label>
<input id="firname" name="firname" class="text" value="" />
</li>
<li><label>Last Name *</label>
<input id="lasname" name="lasname" class="text" value="" />
</li>
<li><label>Email *</label>
<input id="email" name="email" class="text" type="email" value="" />
</li>
<li><label>Phone Number *</label>
<input id="phone" name="phone" class="text" value="" />
</li>
<li><label>Best Time To Call</label>
<select name="best time to call" id="call" class="drop">
<option selected="selected" value="Morning">Morning</option>
<option value="Afternoon">Afternoon</option>
<option value="Evening">Evening</option>
</select>
</li>
<br />
<li><label>Age</label>
<select name="age" id="age" class="drop">
<option selected="selected" value="25+">25+</option>
<option value="21-24">21-24</option>
</select>
</li>
<br />
<li> <label> Rental *</label>
<select name="rental" id="rental" class="drop">
<option selected="selected" value="12 Passenger Van">12 Passenger Van</option>
<option value="15 Passenger Van">15 Passenger Van</option>
<option value="Cargo Van">Cargo Van</option>
</select> </li>
<br />
<li> <label> Pick up Location *</label>
<select name="location" id="location" class="drop">
<option selected="selected" value="Select Location">Select Location</option>
<option value="U-Save JFK">U-Save JFK</option>
<option value="U-Save LGA">U-Save LGA</option>
</select> </li>
<br />
<li><label>Estimated Mileage</label>
<select name="mileage" id="mileage" class="drop">
<option selected="selected" value="Under 100">Under 100</option>
<option value="100-500">100-500</option>
<option value="500+">500+</option>
</select>
</li>
<br />
<li><label>Pick Up Date *</label>
<input id="pick-up-date" name="pick-up-date" class="date" value="MM/DD/YY" /> <a href="javascript:showCal('Calendar1')"><img class="cal" align="absmiddle" src="cal/cal.gif" border="0" alt="Calendar"></a>
</li>
<br />
<li><label>Pick Up Time *</label>
<select class="drop" name="pick up time" id="Pick Up Time">
<option value="12:00 am">12:00 am</option> <option value="12:30 am">12:30 am</option>
<option value="01:00 am">01:00 am</option> <option value="01:30 am">01:30 am</option>
<option value="02:00 am">02:00 am</option> <option value="02:30 am">02:30 am</option>
<option value="03:00 am">03:00 am</option> <option value="03:30 am">03:30 am</option>
<option value="04:00 am">04:00 am</option> <option value="04:30 am">04:30 am</option>
<option value="05:00 am">05:00 am</option> <option value="05:30 am">05:30 am</option>
<option value="06:00 am">06:00 am</option> <option value="06:30 am">06:30 am</option>
<option value="07:00 am">07:00 am</option> <option value="07:30 am">07:30 am</option>
<option value="08:00 am">08:00 am</option> <option value="08:30 am">08:30 am</option>
<option value="09:00 am">09:00 am</option> <option value="09:30 am">09:30 am</option>
<option value="10:00 am">10:00 am</option> <option value="10:30 am">10:30 am</option>
<option value="11:00 am">11:00 am</option> <option value="11:30 am">11:30 am</option>
<option value="12:00 pm">12:00 pm</option> <option value="12:30 pm">12:30 pm</option>
<option value="01:00 pm">01:00 pm</option> <option value="01:30 pm">01:30 pm</option>
<option value="02:00 pm">02:00 pm</option> <option value="02:30 pm">02:30 pm</option>
<option value="03:00 pm">03:00 pm</option> <option value="03:30 pm">03:30 pm</option>
<option value="04:00 pm">04:00 pm</option> <option value="04:30 pm">04:30 pm</option>
<option value="05:00 pm">05:00 pm</option> <option value="05:30 pm">05:30 pm</option>
<option value="06:00 pm">06:00 pm</option> <option value="06:30 pm">06:30 pm</option>
<option value="07:00 pm">07:00 pm</option> <option value="07:30 pm">07:30 pm</option>
<option value="08:00 pm">08:00 pm</option> <option value="08:30 pm">08:30 pm</option>
<option value="09:00 pm">09:00 pm</option> <option value="09:30 pm">09:30 pm</option>
<option value="10:00 pm">10:00 pm</option> <option value="10:30 pm">10:30 pm</option>
<option value="11:00 pm">11:00 pm</option> <option value="11:30 pm">11:30 pm</option>
</select>
</li>
<br />
<li><label>Drop Off Date *</label>
<input id="drop-off-date" name="Drop Off Date" class="date" value="MM/DD/YY" /> <a href="javascript:showCal('Calendar2')"><img class="cal" align="absmiddle" src="cal/cal.gif" border="0" alt="Calendar" /></a>
</li>
<br />
<li><label>Drop Off Time *</label>
<select class="drop" name="Drop Off Time" id="Drop Off Time">
<option value="12:00 am">12:00 am</option> <option value="12:30 am">12:30 am</option>
<option value="01:00 am">01:00 am</option> <option value="01:30 am">01:30 am</option>
<option value="02:00 am">02:00 am</option> <option value="02:30 am">02:30 am</option>
<option value="03:00 am">03:00 am</option> <option value="03:30 am">03:30 am</option>
<option value="04:00 am">04:00 am</option> <option value="04:30 am">04:30 am</option>
<option value="05:00 am">05:00 am</option> <option value="05:30 am">05:30 am</option>
<option value="06:00 am">06:00 am</option> <option value="06:30 am">06:30 am</option>
<option value="07:00 am">07:00 am</option> <option value="07:30 am">07:30 am</option>
<option value="08:00 am">08:00 am</option> <option value="08:30 am">08:30 am</option>
<option value="09:00 am">09:00 am</option> <option value="09:30 am">09:30 am</option>
<option value="10:00 am">10:00 am</option> <option value="10:30 am">10:30 am</option>
<option value="11:00 am">11:00 am</option> <option value="11:30 am">11:30 am</option>
<option value="12:00 pm">12:00 pm</option> <option value="12:30 pm">12:30 pm</option>
<option value="01:00 pm">01:00 pm</option> <option value="01:30 pm">01:30 pm</option>
<option value="02:00 pm">02:00 pm</option> <option value="02:30 pm">02:30 pm</option>
<option value="03:00 pm">03:00 pm</option> <option value="03:30 pm">03:30 pm</option>
<option value="04:00 pm">04:00 pm</option> <option value="04:30 pm">04:30 pm</option>
<option value="05:00 pm">05:00 pm</option> <option value="05:30 pm">05:30 pm</option>
<option value="06:00 pm">06:00 pm</option> <option value="06:30 pm">06:30 pm</option>
<option value="07:00 pm">07:00 pm</option> <option value="07:30 pm">07:30 pm</option>
<option value="08:00 pm">08:00 pm</option> <option value="08:30 pm">08:30 pm</option>
<option value="09:00 pm">09:00 pm</option> <option value="09:30 pm">09:30 pm</option>
<option value="10:00 pm">10:00 pm</option> <option value="10:30 pm">10:30 pm</option>
<option value="11:00 pm">11:00 pm</option> <option value="11:30 pm">11:30 pm</option>
</select>
</li>
<br />
<li><label>Additional Infomation</label>
<textarea id="message" name="message" rows="6" cols="50"></textarea>
</li>
<li class="button1">
<input type="image" name="imageField" id="imageField" src="images/reserve-now-btn.png" class="send" />
<div class="clr"></div>
</li>
</ol>
</form>