Page 1 of 1

Is this possible in PHP?

Posted: Thu Aug 08, 2013 12:27 pm
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?

Re: Is this possible in PHP?

Posted: Thu Aug 08, 2013 1:58 pm
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.

Re: Is this possible in PHP?

Posted: Thu Aug 08, 2013 2:20 pm
by someguyhere
Perfect. Thank you so much. I will give that a spin!

Re: Is this possible in PHP?

Posted: Fri Aug 09, 2013 1:12 am
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?

Re: Is this possible in PHP?

Posted: Fri Aug 09, 2013 6:35 am
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');

Re: Is this possible in PHP?

Posted: Fri Aug 09, 2013 9:40 am
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 );
}

?>


Re: Is this possible in PHP?

Posted: Fri Aug 09, 2013 9:43 am
by someguyhere
Found it...had a misplaced }