Variable of SESSION for a form

0

I need to prevent the assigned filter from being lost when paging, modifying, deleting and / or searching for records. This is one of the 3 input I have assigned to a table in sql, all three share the $result .

How do I add the variable $SESSION to this exercise?

<form action="" method="GET">
    Filtrar por región: 
    <select name="examen1">
        <option>Elija una región ...</option>
        <option value="15">XV Región de Arica y Parinacota</option>
        <option value="1" <?php if(isset($_POST['region']) and $_POST['region'] == 1):?> selected <?php endif;?>>I Región de Tarapacá</option>
        <option value="2" <?php if(isset($_POST['region']) and $_POST['region'] == 2):?> selected <?php endif;?>>II Región de Antofagasta</option>
        <option value="3" <?php if(isset($_POST['region']) and $_POST['region'] == 3):?> selected <?php endif;?>>III Región de Atacama</option>
        <option value="4" <?php if(isset($_POST['region']) and $_POST['region'] == 4):?> selected <?php endif;?>>IV Región de Coquimbo</option>
        <option value="5" <?php if(isset($_POST['region']) and $_POST['region'] == 5):?> selected <?php endif;?>>V Región de Valparaíso</option>
        <option value="13" <?php if(isset($_POST['region']) and $_POST['region'] == 13):?> selected <?php endif;?>>RM de Santiago</option>
        <option value="6" <?php if(isset($_POST['region']) and $_POST['region'] == 6):?> selected <?php endif;?>>VI Región de O´Higgins</option>
        <option value="7" <?php if(isset($_POST['region']) and $_POST['region'] == 7):?> selected <?php endif;?>>VII Región del Maule</option>
        <option value="8" <?php if(isset($_POST['region']) and $_POST['region'] == 8):?> selected <?php endif;?>>VIII Región del Biobío</option>
        <option value="9" <?php if(isset($_POST['region']) and $_POST['region'] == 9):?> selected <?php endif;?>>IX Región de La Araucanía</option>
        <option value="14" <?php if(isset($_POST['region']) and $_POST['region'] == 14):?> selected <?php endif;?>>XIV Región de Los Ríos</option>
        <option value="10" <?php if(isset($_POST['region']) and $_POST['region'] == 10):?> selected <?php endif;?>>X Región de Los Lagos</option>
        <option value="11" <?php if(isset($_POST['region']) and $_POST['region'] == 11):?> selected <?php endif;?>>XI Región de Aysén</option>
        <option value="12" <?php if(isset($_POST['region']) and $_POST['region'] == 12):?> selected <?php endif;?>>XII Región de Magallanes</option>
    </select>
    <input type="submit" value="Buscar">
</form>

<?php 
if (isset($_GET['examen1'])) {
    $PorRegion = ($_GET['examen1']);
    $examen1 = $PorRegion;
    $result = mysqli_query($conn, "SELECT * FROM comunas WHERE region = $PorRegion");
}
?>
    
asked by Natalia 06.09.2016 в 22:21
source

5 answers

1

You do not need the variable session, in your first line you add this:

<?php 
$_GET['region'] = (isset($_GET['examen1']))?$_GET['examen1']:'';
?>

also replaces all _POST with _GET, note that in your FORM the data is sent with the GET method

RESULT:

<?php 
$_GET['region'] = (isset($_GET['examen1']))?$_GET['examen1']:'';
?>
<form action="" method="GET">
        Filtrar por región: 
        <select name="examen1">
            <option>Elija una región ...</option>
            <option value="15">XV Región de Arica y Parinacota</option>
            <option value="1" <?php if(isset($_GET['region']) and $_GET['region'] == 1):?> selected <?php endif;?>>I Región de Tarapacá</option>
            <option value="2" <?php if(isset($_GET['region']) and $_GET['region'] == 2):?> selected <?php endif;?>>II Región de Antofagasta</option>
            <option value="3" <?php if(isset($_GET['region']) and $_GET['region'] == 3):?> selected <?php endif;?>>III Región de Atacama</option>
            <option value="4" <?php if(isset($_GET['region']) and $_GET['region'] == 4):?> selected <?php endif;?>>IV Región de Coquimbo</option>
            <option value="5" <?php if(isset($_GET['region']) and $_GET['region'] == 5):?> selected <?php endif;?>>V Región de Valparaíso</option>
            <option value="13" <?php if(isset($_GET['region']) and $_GET['region'] == 13):?> selected <?php endif;?>>RM de Santiago</option>
            <option value="6" <?php if(isset($_GET['region']) and $_GET['region'] == 6):?> selected <?php endif;?>>VI Región de O´Higgins</option>
            <option value="7" <?php if(isset($_GET['region']) and $_GET['region'] == 7):?> selected <?php endif;?>>VII Región del Maule</option>
            <option value="8" <?php if(isset($_GET['region']) and $_GET['region'] == 8):?> selected <?php endif;?>>VIII Región del Biobío</option>
            <option value="9" <?php if(isset($_GET['region']) and $_GET['region'] == 9):?> selected <?php endif;?>>IX Región de La Araucanía</option>
            <option value="14" <?php if(isset($_GET['region']) and $_GET['region'] == 14):?> selected <?php endif;?>>XIV Región de Los Ríos</option>
            <option value="10" <?php if(isset($_GET['region']) and $_GET['region'] == 10):?> selected <?php endif;?>>X Región de Los Lagos</option>
            <option value="11" <?php if(isset($_GET['region']) and $_GET['region'] == 11):?> selected <?php endif;?>>XI Región de Aysén</option>
            <option value="12" <?php if(isset($_GET['region']) and $_GET['region'] == 12):?> selected <?php endif;?>>XII Región de Magallanes</option>
        </select>
        <input type="submit" value="Buscar">
</form>
    
answered by 06.09.2016 в 23:05
0

If you want to save only the result you would have one thing like this:

 <?php 
        if (isset($_GET['examen1'])) {
            $PorRegion = ($_GET['examen1']);
            $examen1 = $PorRegion;
            $_SESSION['examen1']=$examen1;
            $result = mysqli_query($conn, "SELECT * FROM comunas WHERE region = $PorRegion");
        } 

    ?>

Or you can try sessionStorage

Summary of sessionStorage methods and properties:

  • sessionStorage.setItem ('key', 'value'); Saves the value information that can be accessed by invoking a key. For example, the key can be Francisco's name and value.

  • sessionStorage.getItem('clave') Retrieves the value of the specified key. For example if password is name you can retrieve "Francisco".

  • sessionStorage [key] = value Same as setItem

    etc ...

answered by 06.09.2016 в 22:31
0

The way to store data in $_SESSION is:

  • Initialize the session with session_start()
  • Read / set data with $_SESSION['nombreLlave']
  • In your case, you could establish it before your condition:

    session_start();
    if (!isset($_GET['examen1'])) {
        $_SESSION['examen1'] = $_GET['examen1'];
    }
    
    if (isset($_SESSION['examen1'])) {
        $PorRegion = $_SESSION['examen1'];
        $examen1 = $PorRegion;
        $result = mysqli_query($conn, "SELECT * FROM comunas WHERE region = $PorRegion");
    }
    
        
    answered by 06.09.2016 в 23:24
    0

    At the beginning of the page you must define the session variables.

    <?php
    session_start();
    $seleccion=""
    if (isset($_GET['examen1'])) {
        $_SESSION['examen1'] = $_GET['examen1'];
        $seleccion= $_SESSION['examen1'];
    }
    ?>
    

    Then the in select, you should change the option for something similar to this

    <option value="1" <?php if ($condicion == 1):?> selected <?php endif;?>>I Región de Tarapacá</option>
    
        
    answered by 07.09.2016 в 08:51
    0

    I see that in one place you call it examen1 and in other PorRegion, you should call it the same, for example examen1. In addition, in some cases you use GET and in other POST, use for example GET:

    //Función que devuelve un valor que está guardada en SESSION o se envía por GET
    function getValue($name){
    
        //Asignamos la variable $value a través de la sesión si existe
        $value = (isset($_SESSION[$name])? $_SESSION[$name] : null);
    
        //Si se envía por GET, capturamos la variable y la guardamos en sesión
        if (isset($_GET[$name])) {
            $value = $_GET[$name];
            $_SESSION[$name]= $value ;
        }
        return $value;
    }
    
    
    //Asignamos un array vació a $result por si no hay resultados
    $result = array();
    $examen1 = getValue('examen1');
    
    
    //Si tenemos la variable, sea por SESSION y por GET, hacemos la consulta
    if($examen1){
      $result = mysqli_query(
            $conn, 
           "SELECT * FROM comunas WHERE region = $examen1"
      );
    }
    

    Also, the html could have it like this:

        <?php $examen1 = getValue('examen1'); ?>
        <form action="" method="GET">
        Filtrar por región: 
        <select name="examen1">
            <option>Elija una región ...</option>
            <option value="15" <?php if($examen1 == 15):?> selected <?php endif;?>>XV Región de Arica y Parinacota</option>
            <option value="1" <?php if($examen1 == 1):?> selected <?php endif;?>>I Región de Tarapacá</option>
            <option value="2" <?php if($examen1 == 2):?> selected <?php endif;?>>II Región de Antofagasta</option>
            <option value="3" <?php if($examen1 == 3):?> selected <?php endif;?>>III Región de Atacama</option>
            <option value="4" <?php if($examen1 == 4):?> selected <?php endif;?>>IV Región de Coquimbo</option>
            <option value="5" <?php if($examen1 == 5):?> selected <?php endif;?>>V Región de Valparaíso</option>
            <option value="13" <?php if($examen1 == 13):?> selected <?php endif;?>>RM de Santiago</option>
            <option value="6" <?php if($examen1 == 6):?> selected <?php endif;?>>VI Región de O´Higgins</option>
            <option value="7" <?php if($examen1 == 7):?> selected <?php endif;?>>VII Región del Maule</option>
            <option value="8" <?php if($examen1 == 8):?> selected <?php endif;?>>VIII Región del Biobío</option>
            <option value="9" <?php if($examen1 == 9):?> selected <?php endif;?>>IX Región de La Araucanía</option>
            <option value="14" <?php if($examen1 == 14):?> selected <?php endif;?>>XIV Región de Los Ríos</option>
            <option value="10" <?php if($examen1 == 10):?> selected <?php endif;?>>X Región de Los Lagos</option>
            <option value="11" <?php if($examen1 == 11):?> selected <?php endif;?>>XI Región de Aysén</option>
            <option value="12" <?php if($examen1 == 12):?> selected <?php endif;?>>XII Región de Magallanes</option>
        </select>
        <input type="submit" value="Buscar">
    </form>
    

    Finally, remember that to use sessions you must have the session created:

    session_start(); 
    
        
    answered by 07.09.2016 в 12:00