Jump to content

Recommended Posts

Hello Friends, Please Help. The following the code for simple post method using session. Please read the codes.

 

page1.php

<?php
session_start();

if(isset($_POST['seat']))
{
$seat=$_POST['seat'];
  
     if (isset($_SESSION['seat']))
     { 
	  if ($_SESSION['seat'] == "")
	  {
	      $_SESSION['seat']=$_POST['seat'];
	  }
          else
	  {
	    $_SESSION['seat'] .=",".$_SESSION['seat'];
	  }	
		
      }
      else
      {
	$_SESSION['seat']=$_POST['seat'];
      }	

} 
?>

<html>
<body>

<form name="form1" action="<?php $_PHP_SELF; ?>" method="POST">

Select No. Of Seats:
<select name="seat">
  <option value=""></option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select> 
<input type="submit" name="submit" value="Book Now" />

</form>
<a href = "newpage.php">Results</a>

</body>
</html>


->In Page1.php i was selected the options one by one and clicked the submit. After that i was clicked the 'Results' link which redirect to newpage.php.

newpage.php

<?php
session_start();

if(isset($_SESSION['seat']))
{
  $seat1=explode(",",$_SESSION['seat']);
  foreach($seat1 as $stt)
  {
    echo $stt."<br>";
  }
  
}

session_destroy();
?>

->In newpage.php, the actual result should be 1 2 3 4 5. But i get only the first result like 1 1 1 1 1.

Please help me, what i did a mistake in the coding...???

Link to comment
https://forums.phpfreaks.com/topic/287207-using-session-not-working-properly/
Share on other sites

This

if (isset($_SESSION['seat']))
     { 
	  if ($_SESSION['seat'] == "")
	  {
	      $_SESSION['seat']=$_POST['seat'];
	  }
          else
	  {
	    $_SESSION['seat'] .=",".$_SESSION['seat'];
	  }	
		
      }
      else
      {
	$_SESSION['seat']=$_POST['seat'];
      }	 

should be

if (isset($_SESSION['seat']) && !empty($_SESSION['seat']))
{ 
    $_SESSION['seat'] = array(); // deiine $_SESSION['seat'] as an array
}

$_SESSION['seat'][] = $_POST['seat']; // add selected seat to array

The foreach loop on newpage.php will then be

foreach($_SESSION['seat'] as $stt)
{
    echo $stt."<br>";
}
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.