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 was trying to build a multi-stage form using "if" statements, checking to see if a variable in $_POST was populated before moving on the the next form. The intent was to keep everything on one page, incrementally collecting the data rather than using one long form.
<?php
if ( empty ( $_POST['number'] ) ) {
?>
<form method="post" action="">
<input type="text" name="number">
<input type="submit" value="Next »">
</form>
<?php
}
if ( isset ( $_POST['number'] ) ) {
//store the number in variable, strip HTML tags, and eliminate all nun-numeric characters.
$number = preg_replace( "/[^0-9]/", "", strip_tags( $_POST['number'] ) );
?>
<form method="post" action="">
<input type="text" name="color">
<input type="submit" value="Next »">
</form>
<?php
}
if ( isset ( $_POST['color'] ) ) {
//store the color in variable, strip HTML tags.
$color= strip_tags( $_POST['color'] ) ;
}
?>
But if I echo print_r($_POST) I only get the data from the last form, so the first one is being overwritten. I don't think this should happen since the array elements have different names—am I missing something?
To carry the data across each form you will need to add the posted data to a session or as a hidden input on the next form.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
I tried using sessions, but I don't understand how that's the answer since it's all staying on the same page the whole time—also I can't use session_start(); anyway because this is going to be used inside of Wordpress, and will conflict with the headers there.
I'm lost. Can you explain it like I'm a 4 year old?
I've managed to get it partially working...the problem is that the second form is not storing the data to the session. Is there something obvious that I'm doing wrong here?
<?php
if ( empty ( $_POST['number_people'] ) ) {
?>
<form method="post" action="">
<ul class="blockwrap">
<li class="labelwrap">How many people are in your family or group?<input class="srs-field" type="text" name="number_people"></li>
</ul>
<input type="submit" value="Step 2 »" 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="">
<ul class="blockwrap">
<li class="labelwrap">Drinking water:<input class="srs-field" type="text" name="drinking_water"></li>
</ul>
<input type="submit" value="Step 3 »" style="display: block; margin: 20px 0;">
</form>
<?php
//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'] ) );
}
//Process all data and produce results.
if ( isset ( $_POST['drinking_water'] ) ) {
echo '<span>You have enough water to last ' . $_SESSION['drinking_water']/$_SESSION['number_people'] . ' days.</span><br />';
echo '<span><a href="#">Reset</a></span>';
print_r ( $_SESSION );
unset ( $_SESSION );
}
?>