Why are the data disappearing by refreshing this page?

Asked

Viewed 113 times

1

While I wear that filter for real estate, everything goes well but at the time I refresh the page, the checkbox makes the search disappear. It undoes all the session or at least gives an error in the filter that does not allow to continue recovering the other sessions.

How can I improve this script so that the search is not undone by refreshing the page? I repeat, the checkbox is doing this.

$civil = [];

if(isset($_POST['busca-implacavel'])) {

    if( isset( $_POST['tipo'] ) ) $_SESSION['tipo'] = $_POST['tipo'];
    if( isset( $_POST['civil'] ) ) $_SESSION['civil'] = $_POST['civil'];
    if( isset( $_POST['cidade'] ) ) $_SESSION['cidade'] = $_POST['cidade'];
    if( isset( $_POST['valorminimo'] ) ) $_SESSION['valorminimo'] = $_POST['valorminimo'];
    if( isset( $_POST['valormaximo'] ) ) $_SESSION['valormaximo'] = $_POST['valormaximo'];

    $tipo = isset( $_SESSION['tipo'] ) ? $_SESSION['tipo'] : '';
    $civil = isset( $_SESSION['civil'] ) ? $_SESSION['civil'] : '';
    $cidade = isset( $_SESSION['cidade'] ) ? $_SESSION['cidade'] : '';
    $valorminimo = isset( $_SESSION['valorminimo'] ) ? $_SESSION['valorminimo'] : '';
    $valormaximo = isset( $_SESSION['valormaximo'] ) ? $_SESSION['valormaximo'] : '';

}

Here is the code of the form fields related to checkbox:

<input type="checkbox" name="civil[]" value="solteiro" <?php echo in_array("solteiro", $civil) ? " checked='checked'" : ""; ?>>Solteiro<br>
<input type="checkbox" name="civil[]" value="casado"  <?php echo in_array("casado", $civil) ? " checked='checked'" : ""; ?>>Casado<br>
<input type="checkbox" name="civil[]" value="viuvo"  <?php echo in_array("viuvo", $civil) ? " checked='checked'" : ""; ?>>Viúvo<br>
<input type="checkbox" name="civil[]" value="divorciado"  <?php echo in_array("divorciado", $civil) ? " checked='checked'" : ""; ?>>Divorciado<br>
<input type="checkbox" name="civil[]" value="uniaoestavel"  <?php echo in_array("uniaoestavel", $civil) ? " checked='checked'" : ""; ?>>União Estável<br>

You can see right below print_r($_SESSION) that the system continues to return the índices of array $civil but does not apply the checked.

Thank you :)

1 answer

4


Logical error. You are only recovering the SESSION if there is POST in your code.

The correct part is just the $_POST part within the if, the rest off:

if(isset($_POST['busca-implacavel'])) {
    if( isset( $_POST['tipo'] ) ) $_SESSION['tipo'] = $_POST['tipo'];
    if( isset( $_POST['civil'] ) ) $_SESSION['civil'] = $_POST['civil'];
    if( isset( $_POST['cidade'] ) ) $_SESSION['cidade'] = $_POST['cidade'];
    if( isset( $_POST['valorminimo'] ) ) $_SESSION['valorminimo'] = $_POST['valorminimo'];
    if( isset( $_POST['valormaximo'] ) ) $_SESSION['valormaximo'] = $_POST['valormaximo'];
}

// Daqui pra baixo não depende se tem o POST ou não, então é fora do if.

$tipo = isset( $_SESSION['tipo'] ) ? $_SESSION['tipo'] : '';
$civil = isset( $_SESSION['civil'] ) ? $_SESSION['civil'] : array();
$cidade = isset( $_SESSION['cidade'] ) ? $_SESSION['cidade'] : '';
$valorminimo = isset( $_SESSION['valorminimo'] ) ? $_SESSION['valorminimo'] : '';
$valormaximo = isset( $_SESSION['valormaximo'] ) ? $_SESSION['valormaximo'] : '';

Make sure you have the session_start() at the top of the page, before all this.

One detail: the array you need to initialize like this:

$civil = isset( $_SESSION['civil'] ) ? $_SESSION['civil'] : array();
  • Inattention !!! I was inattentive, it is hunger :)

  • Quite interesting the solution ... I’m learning a lot ... Thanks Bacco ...

Browser other questions tagged

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