S
S
Stepan2014-09-01 15:04:41
PHP
Stepan, 2014-09-01 15:04:41

Am I using the $_SESSION variable correctly?

I can't figure out where I'm doing wrong.
In general: the site is managed by 1s Bitrix.

using sxgeo I determine the city of the visitor. It works. here is the calling code.

<?	
print_r($_SESSION["city"]."work") ;
if(!$_SESSION["city"]){
   include("SxGeo.php");
   $SxGeo = new SxGeo('SxGeo.dat');
   $ip=$_SERVER['REMOTE_ADDR'];
   $city = $SxGeo->getCity($ip);
   $_SESSION["city"] = $rucity = $city['city']['name_ru'];
   echo "
       <div class='alert'>
       ваш город ".$rucity."
       </div>
    ";
}				
?>


the city is determined and everything is good, but there is still such a miracle with a list of cities to which I attached the following function:
function cityselected(){
  city = $('.cityselect').val();
  alert(city);
  $.ajax({
    type: "POST",
    url: "/some.php",
    data: "city="+city+"",
    success: function(msg){
     alert( "Data Saved: " + msg );
    }
  })

  window.location.reload();
  
}


that is, when the value changes, an Ajax request is made to the file in which
<?
unset ($_SESSION['city']);
$_SESSION["city"] = $_POST['city'];
echo "выбран город: ".$_SESSION["city"];
?>


Well, in my view, when the page is reloaded, the selected city should be stored in the $_SESSION["city"] variable, but no, the same city that was set at the very beginning remains. What am I doing wrong? and if not enough information is ready to answer questions.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Burov, 2014-09-01
@ykppon

session_start apparently forgot

T
toxa82, 2014-09-01
@toxa82

window.location.reload(); move it to success: function, otherwise you have a page reload right after the call

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question