Print selected attribute on select option with PHP and MySQL -


i'm printing select list database.

here's part of code:

<form action="" method="post">     <div class="form-item">         <select name="specialties">         <?php         $query = "select * specialties";         $result = mysqli_query($connection, $query);          echo '<option value="all">' . $filtertitle . '</option>';         while($row = mysqli_fetch_assoc($result)) {             $id = $row['id'];             $title = $row['title_'. $lang];              if($_post['specialties'] == $id) {                 $selected = 'selected';             }              echo '<option value="' . $id . ' "' . $selected . '>' . $title . '</option>';         }         ?>           </select>     </div>      <div class="form-item">         <input class="form-submit" type="submit" name="submit" value="<?php echo $filtersubmit; ?>">     </div> </form> 

in form when choose of options, filters content. that's working , don't need post part of code.

first when choose option select list , after submit, filter content, select option shows value, instead of selected option. after i've tried code above , when submit form, select value shows last option , options has selected attribute.

how can change logic, when select let's second option, after submitting form, selected attribute appear on second option, text second option?

conclusion: thank guys answers. you're correct. gave +1 , choose hassaan's answer, because it's explained.

currently checking/comparing values , generating variable. need use else condition because must change/reset value of generated variable. see example below.

if($_post['specialties'] == $id) {     $selected = 'selected'; } else     $selected = ''; 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -