wizardExample.php

  1. <?php
  2. /**
  3.  * Wizard Example
  4.  * wizardExample.php
  5.  * Demonstrate how session variables might be used to facilitate a multi-page web form.
  6.  */
  7.  
  8.  
  9. require_once('FormWizard.class.php');
  10.  
  11. $survey = new FormWizard('survey');
  12. $totalSteps = 3;
  13. $heading = '';
  14.  
  15. if(isset($_POST['fstep']) && is_numeric($_POST['fstep'])) {
  16. $fstep = $_POST['fstep'];
  17. foreach($_POST as $key=>$value) {
  18. // run some validation here ...
  19. // ...
  20. // store to session...
  21. $survey->$key = $value;
  22. }
  23.  
  24. if($fstep == 0) { // form validated and done
  25. // process completed wizard
  26. echo '<pre><code>'.print_r($_SESSION,true).'</code></pre>';
  27. die();
  28. }
  29. } else {
  30. $fstep = 1;
  31. }
  32. echo '<pre>POST: <code>'.print_r($_POST,true).'</code></pre>';
  33. echo '<pre>SESSION: <code>'.print_r($_SESSION,true).'</code></pre>';
  34.  
  35. if($fstep > 0) {
  36. $heading = sprintf('Step %d of %d', $fstep, $totalSteps);
  37. } else {
  38. $heading = 'Finished';
  39. }
  40. echo "<h1>$prompt</h1>";
  41. ?>
  42.  
  43. <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
  44. <?php
  45.  
  46. $disable = 'disabled="DISABLED"';
  47. if($fstep > 1) {
  48. $disable = "";
  49. }
  50. echo '<button type="submit" name="fstep" value="'.($fstep - 1).'" '.$disable.'>Back</button>';
  51. if($fstep < $totalSteps) {
  52. echo '<button type="submit" name="fstep" value="'.($fstep + 1).'">Forward</button>';
  53. } else {
  54. echo '<button type="submit" name="fstep" value="0">Finish</button>';
  55. }
  56.  
  57. /**
  58.  * The forms:
  59.  */
  60. switch($fstep) {
  61.  
  62. case '1' :
  63. ?>
  64. <div class="row"><label for="q1">Question 1</label><input type="text" name="q1" value="<?php echo $survey->q1;?>"></div>
  65. <div class="row"><label for="q2">Question 2</label><input type="text" name="q2" value="<?php echo $survey->q2;?>"></div>
  66. <div class="row"><label for="q3">Question 3</label><input type="text" name="q3" value="<?php echo $survey->q3;?>"></div>
  67. <?php
  68. break;
  69.  
  70. case '2' :
  71. ?>
  72. <div class="row"><label for="q4">Question 4</label><input type="text" name="q4" value="<?php echo $survey->q4;?>"></div>
  73. <div class="row"><label for="q5">Question 5</label><input type="text" name="q5" value="<?php echo $survey->q5;?>"></div>
  74. <div class="row"><label for="q6">Question 6</label><input type="text" name="q6" value="<?php echo $survey->q6;?>"></div>
  75. <?php
  76. break;
  77.  
  78. case '3' :
  79. ?>
  80. <div class="row"><label for="q7">Question 7</label><input type="text" name="q7" value="<?php echo $survey->q7;?>"></div>
  81. <div class="row"><label for="q8">Question 8</label><input type="text" name="q8" value="<?php echo $survey->q8;?>"></div>
  82. <div class="row"><label for="q9">Question 9</label><input type="text" name="q9" value="<?php echo $survey->q9;?>"></div>
  83. <?php
  84. break;
  85. }
  86. ?>
  87. </form>