我有一个表单,其中包含一个包含3个值和其他一些字段的多选列表字段。我想使用php代码在提交按钮后突出显示列表中的选定值。我将如何完成这项任务?
PHP代码: -
<select id="txtconfort" name="txtconfort[]" multiple="multiple" style="width:72%; height:60px;">
<option value="Power Steering">Power Steering</option>
<option value="Power Windows">Power Windows</option>
<option value="Engine Start/Stop Button">Engine Start/Stop Button</option>
</select>
例如。如果我选择Power Steering和Power Windows,那么在提交后我想使用selected属性在相同的文件中突出显示这些值。请帮帮我!
答案 0 :(得分:1)
试试这个:
<select id="txtconfort" name="txtconfort[]" multiple="multiple" style="width:72%; height:60px;">
<option value="Power Steering" <?php if(in_array('Power Steering',$_POST['txtconfort'])) { echo "style='background-coloe:#ccc;'" } ?> >Power Steering</option>
<option value="Power Windows" <?php if(in_array('Power Windows',$_POST['txtconfort'])) { echo "style='background-coloe:#ccc;'" } ?> >Power Windows</option>
<option value="Engine Start/Stop Button" <?php if(in_array('Engine Start/Stop Button',$_POST['txtconfort'])) { echo "style='background-coloe:#ccc;'" } ?> >Engine Start/Stop Button</option>
</select>
答案 1 :(得分:1)
在这里你可以这样做:
<?php
$posted_txtconfort = array();
if(isset($_POST["usubmit"])){
if(isset($_POST["txtconfort"])) $posted_txtconfort = $_POST["txtconfort"];
}
?>
<form method="post">
<select id="txtconfort" name="txtconfort[]" multiple="multiple" style="width:72%; height:60px;">
<option value="Power Steering" <?php if(sizeof($posted_txtconfort) && in_array("Power Steering",$posted_txtconfort)){ echo "SELECTED" ;}?>>Power Steering</option>
<option value="Power Windows" <?php if(sizeof($posted_txtconfort) && in_array("Power Windows",$posted_txtconfort)){ echo "SELECTED" ;}?> >Power Windows</option>
<option value="Engine Start/Stop Button" <?php if(sizeof($posted_txtconfort) && in_array("Engine Start/Stop Button",$posted_txtconfort)){ echo "SELECTED" ;}?> >Engine Start/Stop Button</option>
</select>
<input type="submit" name="usubmit" value="Submit">
</form>
上述代码将确保您在不发布数据时不会收到任何未定义的索引。
答案 2 :(得分:0)
您可以尝试:
<select id="txtconfort" name="txtconfort[]" multiple="multiple" style="width:72%; height:60px;">
<option value="Power Steering" <?php if(in_array('Power Steering',$_POST['txtconfort'])) { echo "selected='true'" } ?> >Power Steering</option>
<option value="Power Windows" <?php if(in_array('Power Windows',$_POST['txtconfort'])) { echo "selected='true'" } ?> >Power Windows</option>
<option value="Engine Start/Stop Button" <?php if(in_array('Engine Start/Stop Button',$_POST['txtconfort'])) { echo "selected='true'" } ?> >Engine Start/Stop Button</option>
</select>
如果数组中存在每个选项,则
echo
selected="true"
为逻辑您也可以使用此循环来呈现选项语句