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!
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.
<?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 »" 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 »" 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'] ) );
?>
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?
<?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 »" 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 »" style="display: block; margin: 20px 0;">
</form>
<?php
}