我的约会表格有问题。当客户填写表单,但必填字段留空时,此字段将收到错误消息。这很好用。问题是这个页面上的不同选择菜单会得到一个奇怪的结构。遇到此问题,将发送错误的日期。如果你想要,你可以在这里测试表格; http://www.o-vita.nl/afspraak.php
表格:
<form action="afspraak_verstuur.php" method="post" name="contact_form">
<table width="300" border="0">
<tr>
<td class="tabform">Naam*:</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<select name="aanhef" type="text" class="text_aanhef" value="Selecteer uw aanhef" />
<option value="Aanhef">Aanhef</option>
<option value="Mevr.">Mevr.</option>
<option value="Dhr.">Dhr.</option>
</select>
<?php echo (isset($_SESSION['aanhef'])) ? $_SESSION['aanhef'] : ""; ?>
<input name="name" type="text" class="text_naam" value="<?php echo (isset($_SESSION['name'])) ? $_SESSION['name'] : ""; ?>" /></td>
</tr>
<?php if (in_array('name', $_GET['error'])) { ?>
<tr>
<td colspan="2">
<font color="red">Er is geen naam ingevuld</font>
</td>
</tr>
<?php } ?>
<tr>
<td class="tabform">E-mail*:</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<input name="email" type="text" class="text_veld" value="<?php echo (isset($_SESSION['email'])) ? $_SESSION['email'] : ""; ?>" />
</td>
</tr>
<?php if (in_array('email', $_GET['error'])) { ?>
<tr>
<td colspan="2">
<font color="red">Er is geen e-mailadres ingevuld</font>
</td>
</tr>
<?php } ?>
<tr>
<tr>
<td class="tabform">Adres:</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<input name="adres" type="text" class="text_veld" value="<?php echo (isset($_SESSION['adres'])) ? $_SESSION['adres'] : ""; ?>" />
</td>
</tr>
<?php if (in_array('adres', $_GET['error'])) { ?>
<tr>
<td colspan="2">
<font color="red">Er is geen adres ingevuld</font>
</td>
</tr>
<?php } ?>
<tr>
<tr>
<td class="tabform">Postcode:</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<input name="postcode" type="text" class="text_veld" value="<?php echo (isset($_SESSION['postcode'])) ? $_SESSION['postcode'] : ""; ?>" />
</td>
</tr>
<?php if (in_array('postcode', $_GET['error'])) { ?>
<tr>
<td colspan="2">
<font color="red">Er is geen postcode ingevuld</font>
</td>
</tr>
<?php } ?>
<tr>
<tr>
<td class="tabform">Woonplaats:</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<input name="woonplaats" type="text" class="text_veld" value="<?php echo (isset($_SESSION['woonplaats'])) ? $_SESSION['woonplaats'] : ""; ?>" />
</td>
</tr>
<?php if (in_array('woonplaats', $_GET['error'])) { ?>
<tr>
<td colspan="2">
<font color="red">Er is geen woonplaats ingevuld</font>
</td>
</tr>
<?php } ?>
<tr>
<tr>
<td class="tabform">Telefoon*:</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<input name="telefoon" type="text" class="text_veld" value="<?php echo (isset($_SESSION['telefoon'])) ? $_SESSION['telefoon'] : ""; ?>" />
</td>
</tr>
<?php if (in_array('telefoon', $_GET['error'])) { ?>
<tr>
<td colspan="2">
<font color="red">Er is geen telefoonnummer ingevuld</font>
</td>
</tr>
<?php } ?>
<tr>
<tr>
<td class="tabform">Datum*:</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<select name="dag" type="text" class="text_dag" value="Selecteer een dag" />
<option value="maandag">maandag</option>
<option value="dinsdag">dinsdag</option>
<option value="woensdag">woensdag</option>
<option value="donderdag">donderdag</option>
<option value="vrijdag">vrijdag</option>
<option value="zaterdag">zaterdag</option>
</select>
<?php echo (isset($_SESSION['dag'])) ? $_SESSION['dag'] : ""; ?>
<select name="dag_nummer" type="text" class="text_dag_nummer" value="1" />
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<?php echo (isset($_SESSION['dag_nummer'])) ? $_SESSION['dag_nummer'] : ""; ?>
<select name="maand" type="text" class="text_maand" value="Januari" />
<option value="januari">januari</option>
<option value="februari">februari</option>
<option value="maart">maart</option>
<option value="april">april</option>
<option value="mei">mei</option>
<option value="juni">juni</option>
<option value="juli">juli</option>
<option value="augustus">augustus</option>
<option value="september">september</option>
<option value="oktober">oktober</option>
<option value="november">november</option>
<option value="december">december</option>
</select>
<?php echo (isset($_SESSION['maand'])) ? $_SESSION['maand'] : ""; ?>
</td>
</tr>
<?php if (in_array('datum', $_GET['error'])) { ?>
<tr>
<td colspan="2">
<font color="red">Er is geen datum ingevuld</font>
</td>
</tr>
<?php } ?>
<tr>
<tr>
<td class="tabform">Tijd*:</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<input name="tijd" type="text" class="text_veld" value="<?php echo (isset($_SESSION['tijd'])) ? $_SESSION['tijd'] : ""; ?>" />
</td>
</tr>
<?php if (in_array('tijd', $_GET['error'])) { ?>
<tr>
<td colspan="2">
<font color="red">Er is geen tijd ingevuld</font>
</td>
</tr>
<?php } ?>
<tr>
<tr>
<td class="tabform">Opmerking:</td>
<td> </td>
</tr>
<tr>
<td height="60" colspan="2">
<textarea name="opmerking" type="text" class="text_comments" rows="4" value="" /><?php echo (isset($_SESSION['opmerking'])) ? $_SESSION['opmerking'] : ""; ?></textarea>
</td>
</tr>
<?php if (in_array('opmerking', $_GET['error'])) { ?>
<tr>
<td colspan="2">
<font color="red">Er is geen opmerking ingevuld</font>
</td>
</tr>
<?php } ?>
<tr>
<td class="tabform">
Nieuwsbrief ontvangen?
<br /><input name="newsletter" type="radio" id="news_true" value="Ja" checked="checked" />
<label for="news_true">Ja</label>
<input type="radio" name="newsletter" id="news_false" value="Nee" />
<label for="news_false">Nee</label>
</td>
<td> </td>
</tr>
<?php if (in_array('name', $_GET['error'])) { ?>
<tr>
<td colspan="2">
Er is geen nieuwsbrief vermelding ingevuld
</td>
</tr>
<?php } ?>
<tr>
<td colspan="2" class="verzenden">
<input name="verzenden" type="image" id="verzenden" src="img/verzenden.png" alt="Verzenden" title="Verzenden" />
</td>
</tr>
</table>
</form>
以下是发送邮件的脚本:
<?php
error_reporting(0);
session_start();
if ("post" === strtolower($_SERVER['REQUEST_METHOD'])) {
$aanhef = $_POST['aanhef'];
$name = $_POST['name'];
$email = $_POST['email'];
$adres = $_POST['adres'];
$postcode = $_POST['postcode'];
$woonplaats = $_POST['woonplaats'];
$telefoon = $_POST['telefoon'];
$dag = $_POST['dag'];
$dag_nummer = $_POST['dag_nummer'];
$maand = $_POST['maand'];
$tijd = $_POST['tijd'];
$opmerking = $_POST['opmerking'];
$newsletter = $_POST['newsletter'];
$_SESSION['aanhef'] = $aanhef;
$_SESSION['name'] = $name;
$_SESSION['email'] = $email;
$_SESSION['adres'] = $adres;
$_SESSION['postcode'] = $postcode;
$_SESSION['woonplaats'] = $woonplaats;
$_SESSION['telefoon'] = $telefoon;
$_SESSION['dag'] = $dag;
$_SESSION['dag_nummer'] = $dag_nummer;
$_SESSION['maand'] = $maand;
$_SESSION['tijd'] = $tijd;
$_SESSION['opmerking'] = $opmerking;
$_SESSION['newsletter'] = $newsletter;
if (empty($name) || empty($email) || empty($telefoon) || empty($tijd)) {
if (empty($name)) {
$errors[] = "name";
}
if (empty($email)) {
$errors[] = "email";
}
if (empty($telefoon)) {
$errors[] = "telefoon";
}
if (empty($tijd)) {
$errors[] = "tijd";
}
header("Location: afspraak.php?error[]=" .implode('&error[]=', $errors));
exit;
} else {
// send mail if (post) parameters are correct
if (mail(
'tim-luijten@hotmail.com',
'Afsprakenformulier o-vita.nl',
"Er is contact opgenomen via het afsprakenformulier. Zie onderstaande gegevens: \n\n
Naam: {$aanhef} {$name} \n
E-mailadres: {$email} \n
Adres: {$adres} \n
Postcode: {$postcode} \n
Woonplaats: {$woonplaats} \n
Telefoonnummer: {$telefoon} \n
Datum: {$dag} {$dag_nummer} {$maand} \n
Tijd: {$tijd} \n
Opmerking: {$opmerking} \n
Nieuwsbrief ontvangen: {$newsletter} \n"
,
"From: {$email} \r\n"
))
{
$_SESSION['aanhef'] = "";
$_SESSION['name'] = "";
$_SESSION['email'] = "";
$_SESSION['adres'] = "";
$_SESSION['postcode'] = "";
$_SESSION['woonplaats'] = "";
$_SESSION['telefoon'] = "";
$_SESSION['dag'] = "";
$_SESSION['dag_nummer'] = "";
$_SESSION['maand'] = "";
$_SESSION['tijd'] = "";
$_SESSION['opmerking'] = "";
$_SESSION['newsletter'] = "";
header("Location: bedankt_afspraak.php");
exit;
} else {
//display a message that the message failed to send.
}
}
}
header("Location: afspraak.php");
?>
我现在有这样的话:
<?php $days = array(
1 => 1,
2 => 2,
3 => 3,
etc
etc
// ...
);
echo '<select name="dag_nummer" type="text" class="text_dag_nummer">' . "\n";
foreach ($days as $value => $text) {
echo '<option value="' . $value . '"';
if (isset($_POST['dag_nummer']) && $_POST['dag_nummer'] == $value) {
echo ' selected="selected"';
}
echo '>' . $text . "</option>\n";
}
echo "</select>\n";
?>
但是,它仍会在下拉框之后显示所选值,而不是选择所需的值... 请参阅我的屏幕截图:http://piclair.com/data/gpjal.jpg
答案 0 :(得分:1)
看起来您希望错误页面显示用户以前为下拉菜单选择的选项。这应该使用以下方法完成:
<option value="10" selected="selected">10</option>
您需要以编程方式处理此选择:
$days = array(
1 => 1,
// ...
);
echo '<select name="dag_nummer" type="text" class="text_dag_nummer">' . "\n";
foreach ($days as $value => $text) {
echo '<option value="' . $value . '"';
if (isset($_POST['dag_number']) && $_POST['dag_number'] == $value) {
echo ' selected="selected"';
}
echo '>' . $text . "</option>\n";
}
echo "</select>\n";
这应该取代您的echo (isset($_SESSION['dag_number'])) ...
逻辑。
还要注意这个构造的<select>
元素的结构 - <option>
元素嵌套在里面。
答案 1 :(得分:0)
在名为“aanhef”的<select>
元素之后,您有这一行:
<?php echo (isset($_SESSION['aanhef'])) ? $_SESSION['aanhef'] : ""; ?>
该行在菜单后打印一些附加文本,通过截图判断 - 看起来你不想要。我认为您可能希望测试每个<option>
元素以查看它是否与$ _SESSION ['aanhef']值匹配,而不是在菜单之后打印该文本,如果是,则追加属性selected="selected"
放入该标记。
另请注意,您的<select>
元素当前在结束标记内部有一个斜杠,如下所示:<select />
。 1}}标签不是自动关闭的,因此不应该存在斜杠。