Check if variable changed, then unset session each time variable changes - PHP -


i read lot of helpful info here when searching answers programming questions. signed because want ask question php variables not able find concise answer on.

i want check if php variable has changed.

i have form field users type city, state into. gets stored in $_session['location'].

i have variable stores country.

when $country variable changes, city, state remain in form field due being in session. issue because city, state entered won't work different country gets selected.

i need able unset $_session['location'] each time country variable changes form field returns empty. i'm not sure how this. here code below attempt of trying unset session in if statement.

// country selector switch ($country) {     case "usa":         $example = "san jose, ca or 94102";         $placeholder = "city, state, or zip";         break;     case "canada":         $example = "toronto, on";         $placeholder = "city, state";         break;     case "india":         $example = "new delhi, delhi";         $placeholder = "city, state";         break;     default:         $example = "san jose, ca or 94102";         $placeholder = "city, state, or zip"; }  // location search $location = ''; $prev_country = $country;  if ($country == $prev_country) {     if (isset($_post['location'])) {         $_session['location'] = $location = ucwords($_post['location']);         header('location: '.($_server['request_uri']));     } elseif (isset($_session['location'])) {         $location = $_session['location'];     } } else {     unset($_session['location']);     unset($_post['location']);     $location = ''; } 

update

i tried suggestions using if ($country == $_session['country']) still getting same result before in previous code.

      // location search   $location = '';   $_session['country'] = $country;   if ($country == $_session['country']) {   if (isset($_post['location'])) {       $_session['location'] = $location = ucwords($_post['location']);           header('location: '.($_server['request_uri']));   } elseif (isset($_session['location'])) {       $location = $_session['location'];   }   } else {       unset($_session['location']);       unset($_post['location']);       $location = '';   } 

this 2 lines of code not going give results condition evaluate true:

$prev_country = $country; if ($country == $prev_country) { 

what need instead, check value have in session against 1 user sent:

if($_session['country'] != $_post['country']) {     // country changed! something! } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -