gpt4 book ai didi

php 表单值不显示

转载 作者:可可西里 更新时间:2023-10-31 22:56:34 24 4
gpt4 key购买 nike

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

这是我的代码:

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

最佳答案

更改此部分:

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 each value”循环的一部分。

关于php 表单值不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13962121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com