4

我的表单不会显示这些值,除非我将它们添加到所需的 = 数组中,并且并非所有内容都是必需的。因此,公司名、姓氏和电子邮件将显示在发送的电子邮件中,但不会显示电话和消息(我还有其他字段要添加)。

这是我的代码:

<?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>
4

3 回答 3

3

更改此部分:

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";
  }
}

至...

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";
}

为了简化,您将获取$values数组中的每个项目并在表单中搜索它。如果它在$required列表中,则检查它是否为空白。如果为空白,则显示错误消息。否则,您希望将其添加到将通过邮件 ( $email_content) 发送的消息中。

以前,“将其添加到电子邮件内容”部分仅在“如果需要”循环中执行。我将它向下移动,使其成为“for eachvalue循环”的一部分。

于 2012-12-19T22:18:26.860 回答
2

您应该从子句中移出该$email_content部分。if(in_array($value,$required)){上一级:

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; }
     }
     // from here
   }
   $email_content .= $value.': '.$_POST[$value]."\n";  // to here
}

因为您不仅希望将必填字段添加到内容中

于 2012-12-19T22:18:06.193 回答
0

您将 a 放置{在错误的位置,导致非必填字段被一起忽略。

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; }
    }
  } // <- Good
  $email_content .= $value.': '.$_POST[$value]."\n";
//  } <- Wrong
}
于 2012-12-19T22:19:47.363 回答