我使用以下代码创建表单。表单包含文本字段,复选框,下拉菜单和文本区域。 我可以在表单提交期间显示文本字段值,如果它正确填充并向未正确填充的fiels显示错误消息。 我想对复选框,下拉列表和文本区域执行相同操作。如果复选框选中YES,则表单应显示复选框,如果表单在表单提交期间包含未填充或填写不正确的字段,则选择YES。 我保存了这样的字段值:
$sender = $_POST["sendername"];
$name_title = $_POST["name_title"];
$pick_up_yes = $_POST["pick_up_yes"];
$pick_up_no = $_POST["pick_up_no"];
$special_req = $_POST["special_req"];
验证:
if(empty($sender)){
//Blank string, add error to $errors array.
$errors['sendername'] = "Please enter your name!";
}
if($name_title === none){
//if selected is none, add error to $errors array.
$errors['name_title'] = "Please select the title of your name!";
}
if (($pick_up_yes != yes) && ($pick_up_no != no)){
//Blank string, add error to $errors array.
$errors['pick_up_no'] = "Please let us know your airport pick up requirement!";
}
HTML表格:
<div class="form-label">Title</div>
<div class="form-input">
<select name="name_title" class="name-title-input">
<option value="none" selected="selected">Select Title</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Miss">Miss</option>
</select>
</div>
<div class="error-msg">
<?php if(isset($errors['name_title'])) { echo '<span style="color: red">'.$errors['name_title'].'</span>'; } ?>
</div>
<div class="form-label">Name</div>
<div class="form-input">
<input type="text" name="sendername" value="<?PHP if(!empty($errors)) { echo $sender;} ?>" />
<div class="error-msg">
<?php if(isset($errors['sendername'])) { echo '<span style="color: red">'.$errors['sendername'].'</span>'; } ?>
</div>
</div>
<div class="form-label">I need Airport pick-up</div>
<div class="form-input">
<input type="checkbox" name="pick_up_yes" value="yes" />Yes
<input type="checkbox" name="pick_up_no" value="no" />No
<div class="error-msg">
<?php if(isset($errors['pick_up_no'])) { echo '<span style="color: red">'.$errors['pick_up_no'].'</span>'; } ?>
</div>
</div>
<div class="form-label">Write us in details if you have any special requirement</div>
<div class="form-input">
<textarea name="special_req" value="<?PHP if(!empty($errors)) { echo $special_req;} ?>"></textarea>
</div>
我得到“sendername”值只有在我再次看到表格时表单提交错误时才会保存。 当表单显示错误时,如何获取所选复选框,下拉列表和文本区域保存/选定值?
答案 0 :(得分:0)
我可能会做的一件事就是改变
if($name_title === none){
要
if($name_title == "none"){
或者只是将其设为空值并改为使用空引号。
另外
if (($pick_up_yes != yes) && ($pick_up_no != no)){
可以
if ($pick_up_yes == ""){
如果将值设置为1表示“是”,将0设置为“否”
要完成你想要的,请说
<?php
if($_POST['inputName'] == 'myInputValue'){
$checked = 'checked';
}
else{
$checked = '';
}
if($_POST['inputName2'] == 'myInputValue2'){
$checked2 = 'checked';
}
else{
$checked2 = '';
}
if($_POST['inputName3'] == 'myInputValue3'){
$selected3 = 'selected';
$selected4 = '';
}
else if($_POST['inputName3'] == 'myInputValue4'){
$selected4 = 'selected';
$selected3 = '';
}
else{
$selected = '';
}
?>
<input type='checkbox' <?= $checked;?> name='inputName' value='myInputValue'/>
<input type='checkbox' <?= $checked2;?> name='inputName2' value='myInputValue2'/>
<select name='inputName3'>
<option <?= $selected3; ?> value='myInputValue3'> pick </option>
<option <?= $selected4; ?> value='myInputValue4'> me </option>
</select>
<textarea name='inputName4'> <?= $special_req; ?> </textarea>
关键是知道您的值在输入中的填充位置,并回显之前通过表单发布的值: - )