Save value of drop-down select in php

2

I have a problem with a select (type combo or dropdown), there I load with php a series of categories, the issue is that by an error of armed of the original base since the categories can not be more than 20/30 , they are always called by their category field (text) and not by their ID. In this case, I charge the select both in value and in the text to be displayed with a string or name of the category, since the query to show the articles of the category I do it by a like.

I choose the category, I make a query, it reloads the page with the new query, and I lose the location of the select, it returns to the first element, although I need to be exactly in the category that was chosen last. Any idea how to do it? I show you the part of the code:

                    <form id="guarda_combo" method="POST" action="categoria.php">
                        <span> Seleccione una Categoría </span>
                        <select id="combo_categorias" name="cat" onchange="location.href='categoria.php?num=' + this.value +'1';">
                <?php
                    if ($result = mysqli_query($con, $consulta)) {
                        /* obtener array asociativo */
                        while ($filas = mysqli_fetch_assoc($result)) {
                            $idCategoria=$filas['idCategoria'];
                            $descCategoria=$filas['descCategoria'];                                
                            echo "<option value='".$descCategoria."'>" . $descCategoria."</option>";
                         }      
                   ?>        
                        </select> 
                    </form>

sanzante, for a question of design, maybe not very good, when choosing a category of the I go category.php which is where I take the value sent and process the view. Right there, I matched the category with a session variable called "cat_elected". I adapted the solution to the following:

                        while ($filas = mysqli_fetch_assoc($result)) {
                            $idCategoria=$filas['idCategoria'];
                            $descCategoria=$filas['descCategoria'];
                            $currentcategoria=$_SESSION["cat_elegida"];
//                            echo "<option value='".$descCategoria."'>" . $descCategoria."</option>";
                            if ($descCategoria == $currentCategoria)
                                {
                                $selected = 'selected="selected"';
                                echo "<option $selected value='".$descCategoria."'>" . $descCategoria."</option>";
                                }else{
                            echo "<option value='".$descCategoria."'>" . $descCategoria."</option>";
                            }
                            }

for some mistake or reason it does not work for me,

    
asked by look68 20.10.2016 в 16:50
source

2 answers

2

You have to indicate in the generated HTML which is the selected option. For this you must use the selected property. For example:

<select name="selector">
  <option>A</option>
  <option selected="selected">B</option>
  <option>C</option>
  <option>D</option>
  <option>E</option>
</select>

In your case:

<?php
  if ($result = mysqli_query($con, $consulta)) {
      /* obtener array asociativo */
      while ($filas = mysqli_fetch_assoc($result)) {
          $idCategoria=$filas['idCategoria'];
          $descCategoria=$filas['descCategoria'];
          $selected = "";                                
          if ($descCategoria === $currentCategoria) {
            $selected = 'selected="selected"';                                
          }
          echo "<option $selected value='".$descCategoria."'>" . $descCategoria."</option>";
      }      
?> 

In the loop if $descCategoria equals $currentCategoria a selected="selected" is added to option current.

Obviously you will have to set in $currentCategoria the category that was selected in the form. For this you will have to consular the array with the data of the form, normally $_POST .

    
answered by 20.10.2016 в 17:13
2

You can send an additional variable and verify, if it exists ... marks as selected the one that is the same when printing the select.

<form id="guarda_combo" method="POST" action="categoria.
    <span> Seleccione una Categoría </span>
    <select id="combo_categorias" name="cat" onchange="location.href='categoria.php?num=' + this.value +'1'&actual='+this.value+';">
    <?php
          if ($result = mysqli_query($con, $consulta)) {
          /* obtener array asociativo */
              while ($filas = mysqli_fetch_assoc($result)) {
                  $idCategoria=$filas['idCategoria'];
                  $descCategoria=$filas['descCategoria'];                                
                  echo "<option value='".$descCategoria."' ".(isset($_GET['actual']) ? 'selected' : "").">" . $descCategoria.</option>";
              }
          }      
    ?>        
    </select> 
</form>

Doing (isset($actual) ? 'selected' : "") verifies if $actual prints selected, but leaves it blank.

    
answered by 21.10.2016 в 16:08