I want my select atribute to change it’s selected option depending on php $choice. For example if I chose FOOD ASC (it’s option 1) i want to make it selected after i press the button with name “sortbutton”
if(isset($_POST['sortbutton']))// if button pressed
{
$choice = $_POST['sort_select'];
if($choice == 0){...there was some code //if selected option == 0
$qr_result = mysqli_query($db, "Select *
from `menu`
WHERE id>0
ORDER BY price ASC")
or die(mysqli_error());
echo '<table class="tam" style = "position:fixed; top: 110px;">'; //table
echo '<td>' . 'Блюдо' . '</td>';
echo '<td>' . 'Состав' . '</td>';
echo '<td>' . 'Цена' . '</td>';
while($data = mysqli_fetch_array($qr_result)){
echo '<tr>';
echo '<td>' . $data['product'] . '</td>';
echo '<td>' . $data['consistency'] . '</td>';
echo '<td>' . $data['price'] . '</td>';
echo '</tr>'; //outputing database
HTML
<form action="" method="POST">
<div class="sorti">
<select name="sort_select" >
<option value="0" name = "value1">PRICE ASC</option>
<option value="1" name = "value2">PRICE DESC</option>
<option value="2" name = "value3">NAME ASC</option>
<option value="3" name = "value4">NAME DESC </option>
</select>
<button name = "sortbutton">Show menu</button>
</form>
Advertisement
Answer
<form action="" method="POST">
<div class="sorti">
<select name="sort_select" >
<option value="0" name = "value1" <?php echo $choice== 0?"selected":""; ?>>PRICE ASC</option>
<option value="1" name = "value2" <?php echo $choice== 1?"selected":""; ?> >PRICE DESC</option>
<option value="2" name = "value3" <?php echo $choice== 2?"selected":""; ?> >NAME ASC</option>
<option value="3" name = "value4" <?php echo $choice== 3?"selected":""; ?> >NAME DESC </option>
</select>
<button name = "sortbutton">Show menu</button>
</div>
</form>I assume , the next page renders the same form again doing above on the html should work. I would probably want to write this better but setting selected on the option is a solution you should go for