Keeping data in the fields after an action or updating on the page - PHP

Asked

Viewed 27 times

2

I would like some help on the following question. When I selected an item in a combobox (it would be for a search filter), I wanted this item to remain selected after pressing the "Query" button. Below, follows a part of the code. If you are missing some information, sorry. Just say so , I provide more information.

<div class="col-md-3">
   <div class="form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12">Sistema</label>
        <div class="col-md-6">
           <select class="form-control" name="sistema">
           <option value="" >Todos</option>
           <?php
             $sql = mysql_query("SELECT * FROM sistema WHERE Codigo = 12 ORDER BY Descricao");
             while ($sistema = mysql_fetch_array($sql)) {
                echo '<option value=' . $sistema['Codigo'] . ' selected>' . $sistema ['Descricao'] . '</option>';
             }
             $sql = mysql_query("SELECT * FROM sistema WHERE Codigo <> 12 ORDER BY Descricao");
             while ($sistema = mysql_fetch_array($sql)) {
                echo '<option value=' . $sistema['Codigo'] . '>' . $sistema['Descricao'] . '</option>';
             }
             ?>
           </select>
        </div>
   </div>
 </div>
  • Because it has 2 querys related to id 12 ? a = and a different ? would be a test

  • Does the "Query" button submit the form? And how is this request, GET or POST? To which URL?

  • One query serves to leave a system selected by default. The other, shows the list of systems that are in the database.

  • is POST request.

  • To leave a default you could put the pure html out of the loop, or check inside the loop if the current system is what you want and leave selected. ($sistema['Codigo'] == 12? 'selected':'')

2 answers

1

I created the conditions so that when the variable $_POST['sistema']: exists and is equal to the Looping id or if it does not exist and the looping id is 12 to option stay as Selected but when there is one $_POST validated it select it.

<div class="col-md-3">
   <div class="form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12">Sistema</label>
        <div class="col-md-6">
           <select class="form-control" name="sistema">
           <option value="" >Todos</option>
           <?php
             $sql = mysql_query("SELECT * FROM sistema ORDER BY Descricao");
             while ($sistema = mysql_fetch_array($sql)) {
                 if(
                     (isset($_POST['sistema']) == true && ($_POST['sistema'] == $sistema['Codigo'])) || (isset($_POST['sistema']) == false && ($sistema['Codigo'] == 12))){
                     echo '<option value=' . $sistema['Codigo'] . ' selected >' . $sistema ['Descricao'] . '</option>';
                 }else{
                    echo '<option value=' . $sistema['Codigo'] . ' >' . $sistema ['Descricao'] . '</option>';
                 }
             }             
             ?>
           </select>
        </div>
   </div>
</div>
  • Show!! It worked out here, buddy. Thank you so much!

1


The excerpt echo '<option '.( (!isset($_POST['sistema']) && $sistema['Codigo'] == 12) || (isset($_POST['sistema'] && $_POST['sistema'] == $sistema['Codigo'] ? 'selected' : '') ).' value=' . $sistema['Codigo'] . '>' . $sistema['Descricao'] . '</option>';

Checks whether the value contained in $_POST['sistema'] is equal to the current system or, if there is no $_POST['sistema'],checks if the current system is 12... If one of these is true, it will bring the selected value.

This will cause the last searched value to be selected, and if there is no value, it will bring the Code 12

<div class="col-md-3">
   <div class="form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12">Sistema</label>
        <div class="col-md-6">
           <select class="form-control" name="sistema">
           <option value="" >Todos</option>
            <?php
             $sql = mysql_query("SELECT * FROM sistema WHERE Codigo <> 12 ORDER BY Descricao");
             while ($sistema = mysql_fetch_array($sql)) {
                echo '<option '.( (!isset($_POST['sistema']) && $sistema['Codigo'] == 12) || (isset($_POST['sistema'] && $_POST['sistema'] == $sistema['Codigo'] ? 'selected' : '' ) ).'   value=' . $sistema['Codigo'] . '>' . $sistema['Descricao'] . '</option>';
             }
             ?>
           </select>
        </div>
   </div>
 </div>

Browser other questions tagged

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