user1632298 user1632298 - 2 months ago 6
PHP Question

Check if select option selected

I have a form where I post some options to a mysql query. When it returns, it a draw a table with some data.

How to set the select box's option to be selected if my form returned?

I tried this, but not working:

<form method="post" id="partnersearchform">
<input type="hidden" name="formaction" value="partnersearch">
Partner Típus<br>
<select onchange="document.getElementById('partnersearchform').submit();" size="" name="ceg">
<option value="">(mind)</option>
<option value="1" if($ceg==1){ print 'selected'; }>Magánszemélyek</option>
<option value="2" if($ceg==2){ print 'selected'; }>Cégek</option>
</select>
</form>

Answer

You forgot to open and close php tags:

<form method="post" id="partnersearchform">
<input type="hidden" name="formaction" value="partnersearch">
Partner Típus<br>
<select onchange="document.getElementById('partnersearchform').submit();" size="" name="ceg">
<option value="">(mind)</option>
<option value="1"<?php if($ceg==1){ print ' selected'; }?>>Magánszemélyek</option>
<option value="2"<?php if($ceg==2){ print ' selected'; }?>>Cégek</option>
</select>
</form>
Comments