Problem with Session variable

Asked

Viewed 558 times

1

On my website, I have a part of the code where two checkbox options should be shown for the check-in date case with difference greater than or equal to 72 hours (or 3 days) compared to today.

<?php
$difdias = (1/86400)*(strtotime($_SESSION['buscaReserva']['dataInicio'])-strtotime(date('Y-m-d')));
if(($_REQUEST['idHotel'] == '373443') && ($difdias >= 3)) {
echo "<tr><td><div id='chkDolars' name='valueDolars' class='checkbox chkSelecionado' style='margin-bottom: 10px'><span class='ph03_105'></span></div><div id='chkReais' name='valueReais' class='checkbox' style='margin-bottom: 10px'><span class='ph03_103'></span></div></td>";
echo "<td><div id='msgReais'><span id='lblReais' class='ph03_104' style='display: none'></span></div></td></tr>";
//echo $_SESSION['buscaReserva']['dataInicio']; 
}
?>

At first, everything is OK, but if I first do a search with check-in greater than 3 days and then do another with check-in less than 3 days, then appear the checkboxes, However, if I repeat this search in the sequence, then the checkboxes disappear (and they should have disappeared the previous time). The same happens if the first search is with check-in less than 3 (there are no checkboxes), I do a second search with check-in greater than 3 and there are no checkboxes and, repeating this second search, end up appearing (when they should have appeared the previous time).

In the commented line of the code, I decided to check how it is coming the variable $_SESSION['buscaReserva']['dataInicio']. I think it takes time to update, and updating the page in new search is usually quick.

I’m not sure how clear the situation is. What I could do in this case to only see the checkboxes in the correct situation (check-in date greater than 3 days) and disappear in the correct situation (check-in date less than 3 days)?

  • Where you declare the variable $_SESSION['searchReserva']['startData'] ??

  • after <?php tried to put session_start Ex: <?php session_start();

  • I imagine this should be solved on the front end, right? With jquery or pure javascript...

  • No, javascript/jquery are not server language so they cannot manage sessions

2 answers

0


To make use of sessions in php you must start the session with the use of session_start(), this both on the screen that puts a value in the session and on the screen that read it follows below an example.

arquivo_setar_sessao.php

<?php
  session_start();//inicio a sessao
  $_SESSION['buscaReserva']['dataInicio'] = "SUA DATA";

arquivo_ler_sessao.php

<?php
  session_start();//inicio a sessao
  echo $_SESSION['buscaReserva']['dataInicio']; //Vai imprimir SUA DATA

Tip here in php.net you find manual on how to make use of session variables.

0

I found the solution. This Session receives the value of a input whose id is dtIni. Using of $_REQUEST['dtIni'] instead of the variable of Session solved my problem.

  • Gustavo, put your answer and then mark it as correct.

Browser other questions tagged

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