Highlight values ​​in a Multi-Select list

I have a form in which there is a select box for several items with three values ​​and some other fields. I want to select the selected values ​​from the list after sending, using php code. How can I complete 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 example, if I select Power Steering and Power Windows, then after sending I want to select these values ​​in the same files using the selected property. Please help me!

+3
source share
3 answers

Try the following:

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

Here you can:

<?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 ensures that you do not get an undefined index if you do not publish the data.

+1
source

:

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

You can use this in a loop also to display option operators

0
source

All Articles