Help with mutli-step form »

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
andrew123
Forum Newbie
Posts: 3
Joined: Sun Oct 02, 2005 4:40 pm

Help with mutli-step form »

Post by andrew123 »

how should i create form like this
have 4questions
Question1--->
checkbox1
checkbox2
checkbox3

if checked will go to Question 2 display a set of options on 2 depends on checkbox is check will display the option for that check box if combine will dispay combine option

if checkbox 1 is check go to 2 display option for checkbox1
if checkbox 2 is check go to 2 display option for checkbox2
if checkbox 3 is check go to 2 display option for checkbox3

Question2--->
new set checkboxes for 1
new set checkboxes for 2
new set checkboxes for 3 plus 1 sepecial selction checkbox

then on Question 2 if check on a special selection checkbox will go to Question 2B

Question 2B--->
radio1
radio2

which has 2 radiobuttons after select and submit will go to Question 2C which has
3 radiobuttons
Question 2C--->
radio1
radio2
radio3
if radiobutton 1 selected will display sometext
if radiobutton 2 selected will go to Question3
if radiobutton 3 selected will go to Question4

well this is waht i have so far stuck at the page 3
i jsut wondering if there is an easier way to do this, what im doing seem to be so complicated

Code: Select all

====1.php==========
<?php 
echo '<pre>';
print_r($_POST);
echo '</pre>';
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
// Get the user's input from the form
   $question1 = $_POST['question1'];

// Register session key with the value
   $_SESSION['question1'] = $question1;

?>

<table><tr><td valign="top" class="head1" width="200"> </td><td>
<br><br>
    Question 1: 
    <br><form action="2.php" method="post">
    <input name="question1[A]" type="checkbox" value="A">A
<input name="question1[B]" type="checkbox" value="B">B
<input name="question1[C]" type="checkbox" value="C">C
<br><br>

<input type="submit" value="Next &raquo;">
</form>
<?php include_once("progressbar.inc"); ?>

Code: Select all

====1.php==========
<?php 
echo '<pre>';
print_r($_POST);
echo '</pre>';
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
// Get the user's input from the form
   $question1 = $_POST['question1'];

// Register session key with the value
   $_SESSION['question1'] = $question1;

?>

<table><tr><td valign="top" class="head1" width="200"> </td><td>
<br><br>
    Question 1: 
    <br><form action="2.php" method="post">
    <input name="question1[A]" type="checkbox" value="A">A
<input name="question1[B]" type="checkbox" value="B">B
<input name="question1[C]" type="checkbox" value="C">C
<br><br>

<input type="submit" value="Next &raquo;">
</form>
<?php include_once("progressbar.inc"); ?>

Code: Select all

====1.php==========
<?php 
echo '<pre>';
print_r($_POST);
echo '</pre>';
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
// Get the user's input from the form
   $question1 = $_POST['question1'];

// Register session key with the value
   $_SESSION['question1'] = $question1;

?>

<table><tr><td valign="top" class="head1" width="200"> </td><td>
<br><br>
    Question 1: 
    <br><form action="2.php" method="post">
    <input name="question1[A]" type="checkbox" value="A">A
<input name="question1[B]" type="checkbox" value="B">B
<input name="question1[C]" type="checkbox" value="C">C
<br><br>

<input type="submit" value="Next &raquo;">
</form>
<?php include_once("progressbar.inc"); ?>
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

you posted your code three times...

this is bad:

Code: Select all

echo '<pre>';
print_r($_POST);
echo '</pre>';
// start the session
session_start();
session_start() has to be called first.

Code: Select all

$question = (isset($_GET['question']) && ctype_digit($_GET['question'])) ? $_GET['question'] : 0;
if ($question == 0) {
  $text = 'what is 1 + 1?';
  $answers = array(2=>1, 5=>2, 3=>3); // answer '2' goes to question 1, answer '5' goes to question 2, etc.
}
elseif ($question == 1) {
  $text = 'what is my name?';
  $answers = array('I forgot'=>4, 'John'=>5);
}
elseif ($question == 2) {
  //...
}
//...
print($text);
foreach ($answers as $answer => $page) {
  print("<input checkbox value='$page'>$answer");
}
That about what you're looking for?
andrew123
Forum Newbie
Posts: 3
Joined: Sun Oct 02, 2005 4:40 pm

Post by andrew123 »

sorry i post worng code page 2 and 3 are here
thanks for your code it look nicer than what i have but im not sure i understand

Code: Select all

=======2.php======

<form action="combo.php" method="post">
<br><br>
<table><tr><td valign="top" class="head1" width="200"> 
<?php 
header("Cache-control: private"); //IE 6 Fix
// Get the user's input from the form
   $question1 = $_POST['question1'];

// Register session key with the value
   $_SESSION['question1'] = $question1;
   
   
if (isset($_POST['question1'])) 
    { 
        echo 'you checked the following boxes:<br>'; 
        foreach ($_POST['question1'] as $value) {echo $value . '<br>';} 
    } 
    else 
    { 
        echo 'you did not check any boxes'; 
    } 
    
    
    ?></td><td>
Question 2: 
    <br><br>

<?php

    if($_POST['question1'][A]==true){
        ?>

<strong>A</strong>
<br>
<input name="A[app1]" type="checkbox" value="app1">app1
<br>
<input name="A[app2]" type="checkbox" value="app2">app2


        
        
        
        <?
    }
    if($_POST['question1'][B]==true){
        ?>
<br><br>
<strong>B</strong>
<br>
<input name="B[test1]" type="checkbox" value="test2">test1
<br><input name="B[test2]" type="checkbox" value="test">test2
<br><input name="B[special]" type="checkbox" value="special">special

        
        <?
    }
    if($_POST['question1'][C]==true){
        ?>
<br><br>
<strong>C</strong>
<br>
<input name="C[pro1]" type="checkbox" value="pro1">pro1
<br><input name="C[pro2]" type="checkbox" value="pro2">pro2
<br><input name="C[Other]" type="checkbox" value="Other">Other


        <?
    }
    


?> 
<br><br>
<input type="button" onclick="javascript:history.go(-1)" value="&laquo; Back">
<input type="submit" value="Next &raquo;">

<?php include_once("progressbar2.inc"); ?>
</td></tr></table>

Code: Select all

=======3.php======
<form action="<?php echo $SERVER['PHP_SELF']; ?>" method="post">
<?php
    if($_POST['B'][special]==true){
        ?>

Question 2b: 
<br><br>
<input name="SingleDual[]" type="radio" value="Single">Single 
<br><input name="SingleDual[]" type="radio" value="Dual">Dual 
<br><br>
<input type="button" onclick="javascript:history.go(-1)" value="&laquo; Back">
<input type="submit" value="Next &raquo;">
<? } 
    else {
        ?>
        
Question 2c: 
    <br><br>
<input name="radio[]" type="radio" value="option1">option1
<br>
<input name="radio[]" type="radio" value="option2">option2
<br>
<input name="radio[]" type="radio" value="Idonotknow">I do not know
<br><br>
<input type="button" onclick="javascript:history.go(-1)" value="&laquo; Back">
<input type="submit" value="Next &raquo;">
        <?
    }

?>

<?php
if ($_POST['radio'][0] == 'Idonotknow') {
        ?>

Question 3: 
<br><br>
<input name="lessthan[]" type="radio" value="less than 10">less than 10
<br><input name="lessthan[]" type="radio" value="lessthan25">less than 25
<br>
<input name="lessthan[]" type="radio" value="less than 50">less than 50
<br>
<input name="lessthan[]" type="radio" value="less than 100">less than 100
<br><br>
<input type="button" onclick="javascript:history.go(-1)" value="&laquo; Back">
<input type="submit" value="Next &raquo;">
<?
} elseif (($_POST['SingleDual'][0]=='Single') || ($_POST['SingleDual'][0]=='Dual')) {
?>

Question 2c: 
    <br><br>
<input name="radio[]" type="radio" value="option1">option1
<br>
<input name="radio[]" type="radio" value="option2">option2
<br>
<input name="radio[]" type="radio" value="Idonotknow">I do not know
<br><br>
<input type="button" onclick="javascript:history.go(-1)" value="&laquo; Back">
<input type="submit" value="Next &raquo;">    
        <?
    }
?> 

</td></tr></table>
Post Reply