Jump to content

Recommended Posts

Hi All,

 

I'm quite new to this PHP stuff and have been creating a site just for the hell of it really but i have a rather bizarre problem that has been driving me nuts for about a week...

 

I have two drops downs, the first gives a list of teams from a mysql table and then this populates the next drop down with a list of players from the selected team.. so far so good.

 

When I hit the select/submit button to select the player, what should happen is both the "team" and "player" are put into another query which gives the stats on the selected player for all the games that he has played in.

 

however, what actually happens in the Session variable sets the "team" variable to blank.

 

I can see this happening when I use

 

Print_r ($_SESSION);

 

So essentially when I click on either submit button from my drop down forms the other value is being removed from the Session array. Oh, the session is still valid (not being destroyed and replaced with another session).

 

 

First drop down

 

                          

                         $query_teams="select Team from $teams";

                          $team_result=mysql_query($query_teams);

                          $num_team=mysql_numrows($team_result);
                                      
                          
                       echo $num_team;
                       print_r ($row_team);
                       
                       $team_count = 0;
                          
                       echo  '<form method="post" action="map_selector.php">';


                       echo "<SELECT name='country'>";
                     
                     while ($row_team1 = mysql_fetch_assoc($team_result))


                               {
                               
                               $team_info = $row_team1["Team"];

                               echo "<OPTION value='$team_info'>$team_info </option>";

                               }
                               

                               echo '</select>';

                               echo '<center><input type="submit" value="Search"></center>';

                          $country = $_POST["country"];

                           $_SESSION["country"] = $country;
                           
                          echo '</form>';

 

So SCOTLAND is selected

 

Some sql queries to get the results for the possible players and then

 

Drop down 2

 

                       $row_full = array_unique($row_home);

                        echo  '<form method="post" action="map_selector.php">';


                       echo "<SELECT name='Player'>";

                       foreach ($row_full as $key => $value)

                               {

                               echo "<OPTION value='$value'> $value";

                               }

                               echo '</select>';
                               
                               echo "@";
                               echo $country;
                               echo "@";

                               echo '<center><input type="submit" value="Search"></center>';

                          $country=$_POST["country"];
                          $player=$_POST["Player"];
                          
                               echo "#";
                               echo $country;
                               echo "#";


                          echo '</form>';

                          $_SESSION["Player"] = $player;

                          $selected_player =  $_SESSION["Player"];

                          echo $player;
                          echo $selected_player;
                          
                          echo $country;

                          
                          Print_r ($_SESSION);

 

so Print_r ($_Session) returns:

 

Array ( [Player] => [country] => Scotland [] => )

 

then a player "Paul Hartley" is selected from the player list and submit is click and then Print_r($_Session) shows:

 

Array ( [Player] => Paul Hartley [country] => [] => )

 

Can anyone please help... I just can;t understand why this is happening when I'm using a Sessions which, I thought, would retain all variable values until a time-out or a destroy

 

Thanks

nhsal69

This is because you are refreshing the page a second time when the second form is submitted and resetting the value of country with the following lines:

 

$country = $_POST["country"];
$_SESSION["country"] = $country;

 

So when you submit the first form the value of $_POST["country"] is stored in the session variable $_SESSION["country"], no problem.

Now, when you submit the second form there is no post data for country (as this form element only exists in the first form), so the the above php is setting the session variable to empty.

 

What you must do is either one of two things:

1. Only set the session variable country when the first form is submitted and the player session variable when the second form is submitted. You can do this using hidden form fields lets say 'form1' and 'form2' in each form and detect them using a conditional statement such as:

if($_POST['form1']) {
$country = $_POST["country"];
$_SESSION["country"] = $country;
}
if($_POST['form2']) {
$player = $_POST["player"];
$_SESSION["player"] = $player;
}

 

2. Store the value on country in a hidden field within form 2

<input type='hidden' name='country' value='<?php print $_SESSION['country']; ?>' />

 

On a side note. It is best practice to do any form data processing at the very top of the script, i.e. prior to you displaying any HTML. So if you are setting session data, storing stuff in a database from a form, etc, always do this at the top of the script. This is because it is not uncommon for you to want to redirect users to another page after you have stored data from a form. You can only do this prior to anything being displayed on the screen. Also it makes your code much more readable.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.