简体   繁体   中英

Highlight values in Multi-select List

I have a form which has a multi-select list field with 3 values and some other fields. I want to highlight selected values from list after submit button using php code. How will I achieve this task?

PHP Code:-

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

For eg. if I select Power Steering and Power Windows then after submit I want to highlight those values in same files using selected property. Please help me!

Try this:

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

Here how you can do:

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

The above code will make sure that you do not receive any undefined index when you do not post the data.

You may try:

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

Logic is to echo selected="true" for every option if it is present in the array

You may use this in loop also to render the option statements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM