Session Variable not sending to next page

I am trying to set a session variable for the location selected in the dropdown menu when the user hits submit. The goal is to pass this variable to another php page so that I can display the drop down option they chose along with two values associated with it from a MYSQL table.

Below is a section of my code from my first php file. It shows the dropdown that is created from a list of values from the MYSQL database along with where I am trying to set the session variable for the selected option.

session_start();
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form> 
<?php
if (isset($_POST['submit'])) { 
  $_SESSION['locations'] = $_POST['locations'];
  $_SESSION['animal']   = 'cat';
} 

Below is my code from the second page where I call the session variables for display. As you can see I have input another variable for testing purposes. It is supposed to display "cat" and it does.

session_start();
echo $_SESSION['animal'];
echo $_SESSION['locations'];  

It may also be important to note that locationsprocessing.php runs a redirect using header. But that is really all that file does.

Am I missing something? I have gone through countless different tuts and such and been staring at this code forever. I can not see why it wo

Answers


you need start session in every pages where you need to use session:

<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form> 
<?php
if (isset($_POST['submit'])) { 
  session_start();
  $_SESSION['locations'] = $_POST['locations'];
  $_SESSION['animal']   = 'cat';
} 

This may be going wrong: When you submit your form in the first piece of code from the questions, you are sending the data entered in the form to /locationsprocessing.php. You say that all that file does is redirecting to another page. That means the data entered in the form is effectively lost.

I think what you want to do is to remove the form processing code from the first piece of code, so with only this remaining:

session_start();
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>

Then place that removed piece of code in /locationsprocessing.php, like this:

session_start();
if (isset($_POST['submit'])) { 
  $_SESSION['locations'] = $_POST['locations'];
  $_SESSION['animal']   = 'cat';
  header('Location: ' . $destination);
}

$destination would be the path for the page with the second piece of code in the question.


Need Your Help

Confusing error from | xargs sed

git bash replace sed xargs

I have a little find-replace utility around git. Let's ignore suggestions for a different approach ATM. I'm really trying to understand this error from sed. Take a look at the command

Creating a sequence in each asp.net GET/POST request

asp.net .net vb.net sequence

I want to create an integer sequence in each page request