Anuj Balan Anuj Balan - 1 year ago 69
PHP Question

php setting value of drop down and radio button with data fetched from database

Started coding in php few days back and am facing following issue.
As the php page loads, am getting details from table and trying to set value of a drop down based on that value that I get from the table.

<select title= "Fruit selected" name="fruit" id="fruit">
<option value="none selected">-Select Fruit-</option>
<?php if(!is_null($user)){ if($user['fruit']=="apple"){echo '<option value="apple" selected="selected">Apple</option>'}}else{echo '<option value="apple">Apple</option>'}?>
<?php if(!is_null($user)){ if($user['grad_semister']=="banana"){echo '<option value="banana" selected="selected">Banana</option>'}}else{echo '<option value="banana">Banana</option>'}?>

I believe am doing fundamentally stupid according to php and I am not good at fundamentals. Can someone throw some light on what could be wrong? As of now when I go to the php page with above code, I get HTTP error 500

Cat Cat
Answer Source

I think you are missing the ; after each echo.

It should be:

<?php if(!is_null($user)){ if($user['fruit']=="apple"){echo '<option value="apple" selected="selected">Apple</option>';}}else{echo '<option value="apple">Apple</option>';}?>
<?php if(!is_null($user)){ if($user['grad_semister']=="banana"){echo '<option value="banana" selected="selected">Banana</option>';}}else{echo '<option value="banana">Banana</option>';}?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download