Keep selected SELECT OPTION after update

Asked

Viewed 386 times

1

I am using a SELECT that automatically updates the page after selection. However it returns with the SELECT at the initial position, when the desired would be that the selected option had already been set in the control.

I found a code on the Internet but I can’t seem to make it work:

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

1 answer

0


The if that has in the 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 suggest you simplify it though. You can use variables directly inside double quotes " without having to concatenate with ..

It can thus transform the echo that has in:

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

Example in Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.