Welcome to WebmasterWorld Guest from 54.162.141.212

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Html PHP multipage form

Need help with multipage form and redirection

     
3:54 pm on Mar 6, 2008 (gmt 0)

5+ Year Member



Hello

I wonder if any of your gurus could help me.

I'm trying to set up a multipage form

The first page consists of 3 fields

Name:
Age:
Living circumstances:

The Living circumstances field is a dropdown consisting of values:

Morgage
Own Outright
Renting

What I would like to do is: If the user selects one of the options from the Living circumstances after pressing the submit button it takes them to a form based on their living circustances. I.e User selects 'Renting' they get redirected to a page called renting-form.php. What I would also like is the information entered into the fields Name and age as Living circumstances get carried over to the selected form page, so they can be added along with the details entered on the new form . I've been reading about hidden fields but have had no success, can someone show me a brief example of how I may do the above.

Thank you in advance.

Tony

5:11 pm on Mar 6, 2008 (gmt 0)

5+ Year Member



The variables from your form get carried to the script you've put in it's action="", and by specifying either method="post" or "get". Then on the script specified in action="", you can get the variables by doing $_GET["variable_name"] and $_POST["variable_name"]. In the form on that page, you should have a php script that uses the variables from $_GET or $_POST to do the <input type="hidden" name="name_of_the_hidden_field" value="value_of_the_hidden_field" /> which should be outputed in a form, and when the form gets submit, they would be sent like if they were textboxes or any other object, except users won't see them.

[edited by: Vis3R at 5:12 pm (utc) on Mar. 6, 2008]

5:12 pm on Mar 6, 2008 (gmt 0)

5+ Year Member



hey Tony,
heres something i just cooked up.

<html>
<head>
<script>
function redirect(form){
try{
var page = form.living.options[form.living.options.selectedIndex].value;
if(page==1){
self.location = "morgage-form.php";
}
else if (page==2){
self.location = "own-form.php";
}
else if (page==3){
self.location = "renting-form.php";
}
}
catch(e){}
}
</script>
</head>
<body>
<form name="form" action="action.php" method="POST">
Name:<input type="text" name="Name"><br>
Age:<input type="text" name="Age"><br>
Living Circumstance:
<select name="living" onchange="redirect(this.form);">
<option selected value="-1">Select a condition</option>
<option value="1">Morgage</option>
<option value="2">Own</option>
<option value="3">Renting</option>
</select>
<input type="button" value="submit" onclick="redirect(this.form);">
</form>
</body>
</html>

As you can see it changes on two events: when the box is changed (onchange) or when the button is clicked (onclick). pretty simply javascript stuff. feel free to remove the js events to your liking.
5:32 pm on Mar 6, 2008 (gmt 0)

5+ Year Member



There are lots of different ways to do this, but I think I would choose sessions if I were doing it myself.

Have the first page form submit to a small processing page that will put all of your form information into a session, then do a header redirect to the page you want according to their selection. The processing page might look like so:

<?php
// Start the session
session_start();

// Fill it with our post vars
$_SESSION['name'] = $_POST['name'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['living'] = $_POST['living'];

// Check the living var and redirect accordingly
if($_POST['living'] == 'mortgage')
header('Location: mortgage.php');
elseif($_POST['living'] == 'own')
header('Location: own.php'):
elseif($_POST['living'] == 'rent')
header('Location: rent.php');

// Just in case someone got here without the form
else
header('Location:default.php');
?>

Then as the user fills out each stage of the form, have the information on each page go into the session. At the end, you can pull all of the cumulative information from the session and do whatever you like with it.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month