Jump to content

PHP and $_SESSION


JJohnsenDK

Recommended Posts

Hey

 

When i make a $_SESSION['test']; should it "be alive" until i kill it with unset()?

 

Because my code doesnt act that way.

 

I have multiple form submits on my page and to remember the data from every form i use sessions. But the session only "survives" doing the one form submit.

 

Here is some of the code:

 

if(isset($_POST['submit_area'])){
    $_SESSION['area'] = $_POST['area'];
}

if(isset($_POST['submit_aktivitet'])){
    $_SESSION['aktivitet'] = $_POST['aktivitet'];
}

 

Here $_SESSION['area'] only lives when i have click the submit_area button.

I have set session_start(); i top of the code.

 

Whats wrong?

Link to comment
https://forums.phpfreaks.com/topic/132713-php-and-_session/
Share on other sites

i see where are going with this, but its not the problem, because i declare ohter sessions with there are no problem with. Im gonna post the code:

 

$_SESSION['area'] echos just fine when i click first 'submit_area' button, but when i click 'submit_aktivitet' button i cant echo anymore, but then i can echo $_SESSION['aktivitet']. And again if i click the last submit (which brings me to a new page) i cant echo $_SESSION['aktivetet'] nor $_SESSION['area'] ??

 

<?php
if(isset($_GET['y'])){
$year = $_GET['y'];	
}else{
$year = date('Y');
}

if(isset($_GET['m'])){
$month = $_GET['m'];	
}else{
$month = date('n');
}

if(isset($_GET['d'])){
$day = $_GET['d'];
}else{
$day = date('j');
}

if(isset($_POST['submit_area'])){
    $_SESSION['area'] = $_POST['area'];
}

if(isset($_POST['submit_aktivitet'])){
    $_SESSION['aktivitet'] = $_POST['aktivitet'];
}

echo "area: ".$_SESSION['area']."<br />".$_SESSION['aktivitet'];

// Sessions
$_SESSION['day']        = $day;
$_SESSION['month']      = $month;
$_SESSION['year']       = $year;

// Date variables
$today = date('j');
$daysInMonth = date("t",mktime(0,0,0,$month,1,$year));
$firstDay = date("w", mktime(0,0,0,$month,1,$year));
$dayOfWeek = date("N", mktime(0,0,0,$month,$day,$year));
$weeksInMonth = 6;

echo $day."/".$month."-".$year." - ".$daysInMonth." - ".$firstDay." - ".$weeksInMonth." - ".$dayOfWeek;

echo "<div style=' margin-left: 20px;color: white'>";
echo "<div style='width: 400px; margin-top: 30px;'>";
if(isset($_SESSION['area'])){
echo "
<div style='float:right; width: 200px;'>
	<form method='post'>
		<select name='aktivitet'>	
			<option value=0>Valg af aktivitet</option>";
			$aktivitetQuery = dbquery("SELECT name, id FROM aktiviteter_kategori WHERE areas_id = '".$_SESSION['area']."'");
			while($aktivitetData = dbarray($aktivitetQuery)){
				echo "<option value=".$aktivitetData['id'].">".$aktivitetData['name']."</option>";
			}
		echo "
		</select>
		<input type='submit' name='submit_aktivitet' value='Vælg' />
	</form>
</div>";
}
echo "
<div style='width: 200px;'>
	<form method='post'>
		<select name='area'>	
			<option value=0>Valg af område</option>";
			$areaQuery = dbquery("SELECT name, id FROM areas");
			while($areaData = dbarray($areaQuery)){
				echo "<option value=".$areaData['id'].">".$areaData['name']."</option>";
			}
		echo "
		</select>
		<input type='submit' name='submit_area' value='Vælg' />
	</form>
</div>";
echo "</div>";

if(isset($_SESSION['aktivitet'])){
echo "<div style='width: 350px; margin-top: 30px;'>";
echo "
<div style='float:right; width: 150px;'>
	<form method='post' action='index.php?site=booking_informationer'>
		<span style='font-weight: bold;'>Antal person:</span> <input type='text' name='persons' style='width: 30px' />
</div>";
echo "
<div width: 150px;'>
		<select name='disciplin'>
			<option value='0' selected=\"selected\">Valg af disciplin</option>";
			$disciplinsQuery = dbquery("SELECT name, id FROM disciplins WHERE aktiviteter_kategori_id = '".$_SESSION['aktivitet']."'");
			while($disciplinsData = dbarray($disciplinsQuery)){
				echo "<option value=".$disciplinsData['id'].">".$disciplinsData['name']."</option>";
			}
		echo "
		</select>
</div>";
echo "</div>";


echo "<div style='margin-top: 40px;'>";
if(isset($_GET['show'])){
echo "<div style='float: right; width: 450px; border: 1px solid white;'>";
echo getBookings($day, $month, $year);
echo "</div>";
}
echo "<div>";

echo "<table style='background-color: #5B5A56; color: black;'>";
echo "<tr><td colspan='8'><table style='background-color:#6F6F6F; color: white;'>";
echo "<tr style='font-size: 12px; font-weight: bold; text-align:center;'><td colspan='8'>".monthToString($month)." ".$year."</td></tr>";
echo "<tr><td>Uge</td><td>Man</td><td>Tirs</td><td>Ons</td><td>Tors</td><td>Fre</td><td>Lør</td><td>Søn</td></tr>";
echo "</table></td></tr>";

$calendar[][] = null;
$days = 1;
for($i=1; $i<$weeksInMonth; $i++){
// Skiftende række farve
if($i % 2 == 0){
	echo "<tr style='background-color: #EDEDED'>";
}else{		
	echo "<tr style='background-color: #FFFFFF'>";
}
// Ugedage
echo "<td style='background-color: white; color: #CBAD3D; border: 1px solid #6F6F6F;  width: 20px; height: 20px;'>$i</td>";
for($d=1; $d<8; $d++){		
	if($days <= $daysInMonth){
		if($days < $today){
			echo "<td class='bookingBookedTD'>$days</td>";
		}else{				
			if($i == 1 && $d < $firstDay){
				echo "<td style='border: 1px solid #EDEDED;  width: 20px; height: 20px;'></td>";	
			}else{
				// Dato booket
				if(checkBookingDate($days, $month, $year)){
					echo "<td class='bookingBookedTD'><a href='index.php?site=booking&d=".$days."&m=".$month."&y=".$year."&show=1'>$days</a></td>";
				}else{
					echo "<td class='bookingTD'><a href='index.php?site=booking&d=".$days."&m=".$month."&y=".$year."&show=1'>$days</a></td>";
				}
			}
		}

		$days++;
	}
}
echo "</tr>";
}

if($month == 12){
$nMonth = 1;
$nYear = $year + 1;
}else{
$nMonth = $month +1;
$nYear = $year;	
}

if($month == 1){
$pMonth = 12;
$pYear = $year - 1;
}else{
$pMonth = $month -1;
$pYear = $year;
}

echo "<tr><td colspan='8'><table style='background-color:#6F6F6F; color: white;'>";
echo "<tr style='font-size: 12px; font-weight: bold; text-align:center; width: 100%;'><td style='width: 33%;'><a href='index.php?site=booking&m=".$pMonth."&y=".$pYear."'>Forrige</a></td><td style='width: 33%;'>Luk</td><td style='width: 33%;'><a href='index.php?site=booking&m=".$nMonth."&y=".$nYear."'>Næste</a></td></tr>";
echo "</table></td></tr>";
echo "</table>";

echo "</div>";

echo "<div>";
echo "<select name='hour'>";
	for($i=getOpeningHours($dayOfWeek); $i <= getClosingHours($dayOfWeek); $i++){
		if(checkBookingHour($i, $day, $month, $year, $_SESSION['aktivitet'])){

		}else{
			echo "<option value='$i'>$i</option>";
		}
	}		
echo "</select>";
echo "<input type='submit' name='submit_booking' value='Næste' />";
echo "</form>";
echo "</div>";
echo "</div>";
}
echo "</div>";

?>

Link to comment
https://forums.phpfreaks.com/topic/132713-php-and-_session/#findComment-690207
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.