Is this possible in PHP?

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

Is this possible in PHP?

Post by someguyhere »

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.

For example

Code: Select all

<?php 

if ( empty ( $_POST['number'] ) ) {

?>

<form method="post" action="">
<input type="text" name="number">
<input type="submit" value="Next &raquo;">
</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 &raquo;">
</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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Is this possible in PHP?

Post by AbraCadaver »

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.
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: Is this possible in PHP?

Post by someguyhere »

Perfect. Thank you so much. I will give that a spin!
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: Is this possible in PHP?

Post by someguyhere »

I'm having no luck...

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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Is this possible in PHP?

Post by Celauran »

someguyhere wrote:also I can't use session_start(); anyway because this is going to be used inside of Wordpress
Sure you can. Something like this:

Code: Select all

function my_session() {
    if (!session_id()) {
        session_start();
    }
}
add_action('init', 'my_session');
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: Is this possible in PHP?

Post by someguyhere »

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?

Code: Select all


<?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 &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="">
<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 &raquo;" 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 );
}

?>

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

Re: Is this possible in PHP?

Post by someguyhere »

Found it...had a misplaced }
Post Reply