0

I read a lot of helpful info here when searching for answers to my programming questions. I just signed up because I want to ask a question about PHP variables that I am not able to find a concise answer on.

I want to check if a PHP variable has changed.

I have a form field that users type their city, state into. It gets stored in $_SESSION['location'].

I also have a variable that stores country.

When the $country variable changes, the city, state remain in the form field due to it being in session. This is an issue because obviously the city, state originally entered won't work for a different country that gets selected.

I need to be able to unset the $_SESSION['location'] each time the country variable changes so that the form field returns to empty. I'm not sure how to do this. But here is my code below with an attempt of trying to unset the session in my 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']) but still getting same result as 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 = '';
  }

2 Answers 2

1

This 2 lines of code are definitely not going to give any good results as the condition will always evaluate to true:

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

What you need to do instead, is to check the value that you have in session against the one that the user just sent:

if($_SESSION['country'] != $_POST['country']) {
    // country changed! do something!
}
Sign up to request clarification or add additional context in comments.

4 Comments

$country variable is a variable I set on the page. It does not come from $_POST.
Isn't the user choosing the country on the webpage? If yes, send the chosen value along with the other data, if not, well, you have some sort of algorithm that decides what country the user selected, right? Let's name it get_country, then call it and compare it against the previous value in the session.
I have not yet setup to let the user select the country. So for now I am just declaring the country in a variable called $country and it changes based on specific pages the user navigates to.
"it changes based on specific pages the user navigates to" - well that's your algo
0

This doesn't make any sense.

$prev_country = $country;

if ($country == $prev_country) {

The if statement will always be true, since $country is the same as $prev_country. You should rather check if $country is the same as previously in your session (save it there if you don't do it until now).

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 = '';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.