Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multipage form navigation

Status
Not open for further replies.

patfee

Programmer
Dec 14, 2004
78
NL
Hi,
proceeding to a following form by posting variable data as hidden fields in the second form works well.

But could someone sugest how to manage the same trick if i want to include a "previous" button to go back to my first form while maintaining the form data?

So lets say i have two forms.
After completing the first form i navigate to the second. Field data is being posted to the second form and included as hidden fields. I enter some data on the second form and deside to go back to the first form by using the a previous button. The first form should display the previous entered data, and when i continue to the second form, the previous entered data should be there again as well... is that possible?

Thansk
Patrick

 
don't store data as hidden fields. it is prone to end-user manipulation.

store the data as session variables instead.

when you go back (back button) simply check for the existence of session data for that form and if there is some present, prefill the form for the user.
 
How would i proceed please?

something like this?

session_start();
if(isset($_SESSION['naam'])) $_SESSION['naam']=$_SESSION['naam']; else $_SESSION['views']="";
?>

<form action="self" method="post" name="Formname" target="_self">
<input name="naam" type="text" size="30" maxlength="255" />
<input name="Next" type="button" value="Submit" />
</form>
 
nearly there. more like this, i'd advise (sorry i have not had time to put comments into the code).

Code:
<?
session_start();
if (isset ($_POST['submit'])){
	switch($_POST['submit']){
		case "Next":
			//submission of form 1
			$_SESSION['form1'] = serialize($_POST);
			displayForm2();
			break;
		case "Save Data":
			//submission of form 2
			$form2data = $_POST;
			if (!isset($_SESSION['form1'])){
				die("problem here");
			}
			$form1data = unserialize ($_SESSION['form1']);
			$data = array_merge ($form1data, $form2data);
			processForms($data);
			break;
		case "Back...":
			if (isset($_SESSION['form1'])){
				$form1data=unserialize($_SESSION['form1']);
			}
			displayForm1($form1data);
			break;
		default:
			displayForm1();
	}
} else {
	displayForm1();
}


function displayForm1($array=array()){
$naam = (isset($array['naam'])) ? $array['naam'] : ''; 
echo '
<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="Formname" target="_self">
Name: <input name="naam" type="text" size="30" maxlength="255" value="'.$naam.'"/>
<input name="submit" type="submit" value="Next" />
</form>';
}
function displayForm2() {
echo '
<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="Formname" target="_self">
TelNum: <input name="telnum" type="text" size="30" maxlength="255" value=""/>
<input name="submit" type="submit" value="Back..." />
<input name="submit" type="submit" value="Save Data" />
</form>';
}
function processForms($array){
	echo '<pre>'.print_r($array, true) . '</pre>';
}
?>
 
Hi,
Thanks for pushing me in the right direction!

Just one other thing. Testing the script locallay on my Wamp 1.7 server gives the following errors:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\wamp\ in C:\wamp\ on line 11

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\ in C:\wamp\ on line 11

Any ideas please?

Thanks again
Patrick
 
Just tested it online with my host, and no errors related to the session thing there...

An other problem there is that it does not remeber the value on page one, after i jump back to it from page two.

Any sugestions please?

thank
Patrick
 
on the three installations that i have, the settings are remembered.

be certain that
*sessions are working properly on your host
*the session save path is correctly set on your host
*you have error reporting turned up high and display errors turned on to see the errors
*there is nothing in your server logs that can help

it may be that you are getting a race condition on the session files, but this would be unlikely on a remote host. to be certain add
Code:
session_write_close();
just BEFORE the function displayform1 code

 
Thanks,
Works after including the session_write_close(); statement.

Other question... i'm trying to implement this on my Joomla platform, and was wondering if the above code could be distrubuted over serveral pages / files?

i.e. form_1 opens a session and the user enters the values on form 1. After pressing the Next page button, the form_2 page opens (physical different php file) with a previous page button (pointing to form_1), a next page (pointing to form_3, which also is a physical different file) and some user inputs. Finally form_3 includes a submit button, which submits the form_1, 2 and 3 user input.

this specific setup is by the way to use with the joomla chronoforms component... unfortunately, no examples exist on their forum...

Thanks
Patrick
 
euuee... i'm quite new to this php form business... could you give an example maybe?

Thanks
Patrick
 
it's really just the same. there are ways to automate this but they (ideally) involve prototyping each form. this has benefits because the form can then be generated, processed and validated dynamically.

once you decide to start prototyping forms you can take a look at html_quickform and formBuilder (within the pear repository).

Code:
<?
session_start();

if (isset ($_POST['submit'])){
    switch($_POST['submit']){
        case "Next":
			$formname = isset($_POST['formname']) ? $_POST['formname'] : '';
            //submission of form
			//save form data to session
            $_SESSION[$formname] = serialize($_POST);
            switch ($formname) {
				case "form1":
					displayForm2();
					break;
				case "form2":
					displayForm3();
					break;
			}
            break;
        case "Save Data":
            //submission of form 2
            $form3data = $_POST;
            if ( !isset($_SESSION['form1']) || !isset($_SESSION['form2']) ){
                die("problem here");
            }
            $form1data = unserialize ($_SESSION['form1']);
			$form2data = unserialize ($_SESSION['form2']);
            $data = array_merge ($form1data, $form2data, $form3data);
            processForms($data);
            break;
        case "Back...":
			$formname = isset($_POST['formname']) ? $_POST['formname'] : '';
			switch ($formname) {
				case "form2":
					if (isset($_SESSION['form1'])){
						$formdata=unserialize($_SESSION['form1']);
					} else {
						$formdata = array();
					}
            		displayForm1($formdata);
				break;
				case "form3":
					if (isset($_SESSION['form2'])){
						$formdata=unserialize($_SESSION['form2']);
					} else {
						$formdata = array();
					}
            		displayForm2($formdata);
				break;
			}
            break;
        default:
            displayForm1();
    }
} else {
    displayForm1();
}

session_write_close();

function displayForm1($array=array()){
$naam = (isset($array['naam'])) ? $array['naam'] : ''; 
echo '
<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="Formname" target="_self">
<input type="hidden" name="formname" value="form1" />
Name: <input name="naam" type="text" size="30" maxlength="255" value="'.$naam.'"/>
<input name="submit" type="submit" value="Next" />
</form>';
}
function displayForm2($array=array()) {
$telnum = isset($array['telnum']) ? $array['telnum'] : '';
echo '
<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="Formname" target="_self">
<input type="hidden" name="formname" value="form2" />
TelNum: <input name="telnum" type="text" size="30" maxlength="255" value="'.$telnum.'"/>
<input name="submit" type="submit" value="Back..." />
<input name="submit" type="submit" value="Next" />
</form>';
}
function displayForm3() {
echo '
<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="Formname" target="_self">
<input type="hidden" name="formname" value="form3" />
TelNum2: <input name="telnum2" type="text" size="30" maxlength="255" value=""/>
<input name="submit" type="submit" value="Back..." />
<input name="submit" type="submit" value="Save Data" />
</form>';
}
function processForms($array){
    echo '<pre>'.print_r($array, true) . '</pre>';
}
?>
 
Thanks again Jpaddie!

Not exactly what i was after (wanted to distribute each for in a separate file), but anyway, i used a workaround in Joomla, using a wrapper, and this works perfect.

Last question, instead of preprinting, what should i change to email the data?

Cheers
Patrick
 
i don't know what you mean by "distribute each for[m] in a separate file". if you mean you want to put each form code in a separate logical file on your server then that is still not problem. just change eg the displayForm2() function to be

Code:
function displayForm2($array = array()){
$telnum = isset($array['telnum']) ? $array['telnum'] : '';
include 'path/to/form2.php';
}

make sure that the form action in form2 points back to the main dispatcher for the forms (the code above).

to change the processing of the forms, just put your code into the processForms() function

Code:
function processForms($array) {
 $toemail = ''; //to fill in 
 $fromemail = ''; //to fill in
 mail ($toemail, $subject, print_r($array,true), "From: ".$fromemail);
}
 
Thanks JPadie!
I will elaborate on this distribute thing a little later.

What about checkboxes and radio groups?

I.e. including the code below does not remember any value for the checkbox when i navigate back. Worse than that, if i submit data, no checkbox values are printed as well

under the function declaration:
$check = (isset($array['check'])) ? $array['check'] : '';

Within the form echo bit:
checkbox test: <input name="checkbox" type="checkbox" value="'.$check.'" />

For a radio group i'm even more lost... as the different radio values have a different "value"

<p>
<label>
<input type="radio" name="Group1" value="v1" />
Value1</label>
<br />
<label>
<input type="radio" name="Group1" value="v2" />
value 2</label>
<br />
</p>

Sorry for me being so stupid hahaha, but its a complete change in coding to what i'm used to through VB, and there is hardly some suitable material existing on the well...

Thanks
Patrick
 
Worse than that, if i submit data, no checkbox values are printed as well
yup - that's how forms work. if a control is not 'checked' then the browser will not submit it.

radio groups and select boxes work in similar ways to create stickiness
Code:
<? $radiovalue = (isset($_POST['radiovalue'])) ? $_POST['radiovalue'] : ''; ?>
<input type="radio" value='1' <? if ($radiovalue=='1'){echo "checked=\"checked\"";}> some text

there is lots of stuff on the web about sticky forms.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top