$_SESSION array storage issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

$_SESSION array storage issue

Post by someguyhere »

I can't tell what I'm doing wrong here. This form is a multi-stage form on one page; the first form is supposed to be displayed only if $_SESSION['number_people'] is empty. It shows up fine when you first load the page, but when you fill out the form and hit "Next" the page reloads and displays both the first form AND the second form. If you then fill out the second form and click "Next" the first form never shows up again.

The code below is the portion from the beginning, up to just after the problem goes away.

Can someone tell me what I'm doing wrong here?

Code: Select all


<?php 

if ( empty ( $_SESSION['number_people'] ) ) {

?>

<form method="post" action="">
<h3>Community</h3>
<ul>
	<li>How many people are in your family or group? <input class="srs-field" type="text" name="number_people"></li>
</ul>
<input type="submit" value="Next &raquo;" style="display: block; margin: 20px 0;">
</form>

<?php

}

if ( isset ( $_POST['number_people'] ) ) {

	//store the number of people in variable, strip HTML tags, and eliminate all nun-numeric characters.

		$_SESSION['number_people'] = preg_replace( "/[^0-9]/", "", strip_tags( $_POST['number_people'] ) );

?>

<form method="post" action="">
<h3>Water Storage</h3>
<ul>
	<li>How much drinking water, in gallons, do you have stored? <input  class="srs-field" type="text" name="drinking_water"></li>
</ul>
<input type="submit" value="Next &raquo;" style="display: block; margin: 20px 0;">
</form>

<?php

}

if ( isset ( $_POST['drinking_water'] ) ) {

	//store the gallons of water in variable, strip HTML tags, and eliminate all nun-numeric characters.

		$_SESSION['drinking_water'] = preg_replace( "/[^0-9]/", "", strip_tags( $_POST['drinking_water'] ) );

?>


User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: $_SESSION array storage issue

Post by requinix »

You set the number_people after the code that checks to see if it's set.

General principle with stuff like this: do the form processing before you show the forms. Completely separate.
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: $_SESSION array storage issue

Post by someguyhere »

Awesome, I've revised it per your advice and that solved that problem, but seems to have created a new one. Previously, each form only displayed after the previous one had been submitted. Now they all display at once, and then disappear one by one upon submission. Would using if/else be the answer?

Code: Select all


<?php 

if ( isset ( $_POST['number_people'] ) ) {

	//store the number of people in variable, strip HTML tags, and eliminate all nun-numeric characters.

		$_SESSION['number_people'] = preg_replace( "/[^0-9]/", "", strip_tags( $_POST['number_people'] ) );

}

if ( is_null ( $_SESSION['number_people'] ) ) {

?>

<form method="post" action="">
<h3>Community</h3>
<ul>
	<li>How many people are in your family or group? <input class="srs-field" type="text" name="number_people"></li>
</ul>
<input type="submit" value="Next &raquo;" style="display: block; margin: 20px 0;">
</form>

<?php

}

if ( isset ( $_POST['drinking_water'] ) ) {

	//store the gallons of water in variable, strip HTML tags, and eliminate all nun-numeric characters.

		$_SESSION['drinking_water'] = preg_replace( "/[^0-9]/", "", strip_tags( $_POST['drinking_water'] ) );

}

if ( is_null ( $_SESSION['drinking_water'] ) ) {

?>

<form method="post" action="">
<h3>Water Storage</h3>
<ul>
	<li>How much drinking water, in gallons, do you have stored? <input  class="srs-field" type="text" name="drinking_water"></li>
</ul>
<input type="submit" value="Next &raquo;" style="display: block; margin: 20px 0;">
</form>

<?php

}

someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: $_SESSION array storage issue

Post by someguyhere »

Just solved the last part...

Code: Select all


if ( isset ( $_SESSION['number_people'] ) && empty ( $_SESSION['drinking_water'] ) ) {

Thanks for your help on the other issue! :D
Post Reply