Keep SELECT OPTION selected after upgrade

1

I'm using a SELECT that automatically refreshes the page after the selection. However it returns with the SELECT in the initial position, when it would be desirable that the selected option had already been set in the control.

I found a code on the internet but I can not seem to make it work:

echo "<option value='premorc.php?fnc=". $fnc . "&ans=" . $linha . "&bandorc=" . $band . "'  if ($bandorc==$band){ echo 'SELECTED';} '>" . $bandeira . "</option>";
    
asked by anonymous 24.10.2017 / 22:05

1 answer

0

The if that has echo is being interpreted as text.

echo "<option value='premorc.php?fnc=".$fnc."&ans=".$linha."&bandorc=".$band."'  if ($bandorc==$band){ echo 'SELECTED';} '>" . $bandeira . "</option>";
//----------------------------------------------------- faltou concatenar aqui ^

I would, however, suggest simplification. You can use variables directly within double quotation marks " without having to concatenate with . .

So you can transform the echo you have into:

echo "<option value='premorc.php?fnc=$fnc&ans=$linha&bandorc=$band' ".($bandorc==$band?"selected":"").">$bandeira</option>";

Example on Ideone

    
25.10.2017 / 00:04