How to persist $_SESSION value in <select> as it was done for <input>?

Asked

Viewed 785 times

3

I’m trying to get the value of select in the $_SESSION to persist the search data on a system I’m doing, but I don’t know how to apply this method to the select. Can you help me?

I’ll save the variables after the $_POST...

<?php 

if($_POST) {
    $_SESSION['nome'] = $_POST['nome'];
}

?>

How to save the select just as I kept the input?

<?php
if(isset($_SESSION['nome'])){
    echo '<input type="text" name="nome" value="'.$_SESSION['nome'].'" class="form-control">';
} else {
    echo '<input type="text" name="nome" value="" class="form-control">';
}
?>
<select name="tipo" class="form-control" required>
    <option value="">Tipo</option>
    <option value="apartamento">Apartamento</option>
    <option value="casa">Casa</option>
    <option value="flat">Flat</option>
</select>

2 answers

5


If I understand the question, it’s more or less that:

<?php
   $s = ' selected="selected" ';
   $nome = isset( $_SESSION['nome'] ) ? $_SESSION['nome'] : '';
   $tipo = isset( $_SESSION['tipo'] ) ? $_SESSION['tipo'] : '';
?>
<input type="text" name="nome" value="" class="form-control">
<select name="tipo" class="form-control" required>
    <option value="">Tipo</option>
    <option <?php echo $tipo=='apartamento'?$s:''; ?>value="apartamento">Apartamento</option>
    <option <?php echo $tipo=='casa'       ?$s:''; ?>value="casa">Casa</option>
    <option <?php echo $tipo=='flat'       ?$s:''; ?>value="flat">Flat</option>
</select>

That is, only the option previously selected will receive the parameter selected.

If you need to check the post, this is the complete code:

<?php
   session_start();
   if( isset( $_POST['nome'] ) ) $_SESSION['nome'] = $_POST['nome'];
   if( isset( $_POST['tipo'] ) ) $_SESSION['tipo'] = $_POST['tipo'];

   $s = ' selected="selected" ';
   $nome = isset( $_SESSION['nome'] ) ? $_SESSION['nome'] : '';
   $tipo = isset( $_SESSION['tipo'] ) ? $_SESSION['tipo'] : '';
?>
<input type="text" name="nome" value="" class="form-control">
<select name="tipo" class="form-control" required>
    <option value="">Tipo</option>
    <option <?php echo $tipo=='apartamento'?$s:''; ?>value="apartamento">Apartamento</option>
    <option <?php echo $tipo=='casa'       ?$s:''; ?>value="casa">Casa</option>
    <option <?php echo $tipo=='flat'       ?$s:''; ?>value="flat">Flat</option>
</select>
  • That’s exactly it !!!! The paths of the other answer helped me to get where I wanted. Thank you.

  • 2

    I thought Alisson’s explanation was good, but I had the impression that his question was more about select than about Session.

  • I will ask this question to checkbox ... help me Bacco ... Thank you !!!

  • 1

    I misread the term ... it’s checkbox because it involves array and selection of various elements ... so I want to reproduce this question to checkbox. What do you think?

3

It’s the same way $_POST['nome'] only by changing the value.

Try

<?php 

if(isset($_POST)) {
    $_SESSION['nome'] = $_POST['nome'];
    $_SESSION['tipo'] = $_POST['tipo'];
}

Don’t forget to put session_start() at the beginning of the login code.

To check if there is

<?php

if(!empty($_SESSION['tipo'])){
     echo 'existe';
}else{
     echo 'não existe';
}
  • You can increment the answer by adding the if/Else in the select for me to see the structure?

  • @Marcosvinicius sorry, I did not understand the question

  • You saw I did the if/Else in the input to detect if there is value in Session nome ... I wonder how it’s done in the select !!! Thank you

  • Ready @Marcosvinicius . I used the function empty to check that the session is not empty, because sometimes it can be created and have its value empty, then Empty does this check. It’s basically almost equal to isset

  • How can that be improved? Anyone visiting this question, this link is a functional version to store fields of a search - http://pastebin.com/XizWNWbh

  • A tip is you put the inputs with required to have no chance of the user not typing anything and even so when you click the Submit button he record the session empty. Another thing, a hint too, is in the Submit you put the name and in the invez IF check only if($_POST) check like this if($_POST['nome_do_sumit']) . I think it’s safer to do it this way, because then you know he actually clicked the button.

  • I’m learning this today. Thank you so much for your help. :)

Show 2 more comments

Browser other questions tagged

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