xTRIFAC70Rx xTRIFAC70Rx - 3 months ago 21
MySQL Question

Remeber Dropdown Selection Choice after Search Laravel

I have a few dropdown box filters , once you click submit it searches based on what you selected. When the results have loaded, the dropdown boxes reset back to how they were originally opposed to remembering what you previously selected.

This is what they look like.

<select id="buyer" name = "buyer" class="form-control" style="width: 100%" data-placeholder="">
<option value="" >Buyer</option>
<?php

foreach($buyer as $key){
echo '<option value=' . $key->LenderName . ">" . $key->LenderName . "</option>";

}
?>
</select>


How do get the dropdown box to remember the previous choice after search.

Answer

You need to say what option needs to be selected after submit, in your case during the loop if the value of "for" is equal with the "buyer" value posted, then add

selected

attribute in your :

<select id="buyer" name = "buyer" class="form-control" style="width: 100%" data-placeholder="">
    <option value=""  >Buyer</option>
    <?php

    foreach($buyer as $key){   
    echo '<option value=' . $key->LenderName;
    if ($_POST['buyer']==$key->LenderName) { 
    echo " selected";
    }
    echo ">" . $key->LenderName . "</option>";

    }   
    ?>   
    </select>