Jump to content

Multiple form submission and retrieve all data at the end


naruttam
 Share

Recommended Posts

Hello Experts,

I am trying to make a quiz contest in processwire. This quiz needs to show one question per page. The aim of this quiz is to get more page views. so I created a template, where Quiz questions can be uploaded. After answering one question the user is redirected to the next question or URL on clicking the submit button. I am unable to get the form data. I have the form template and the final page to evaluate the scores. I dont understand where I am going wrong. Here is my code.

Please help me out..... :'(

quiz.php

<?php include("./head.inc"); 
include("./functions.php");

?>

<div id="content" class="row inside search-page">      <!-- content starts -->
  <div class="content-border">                   <!-- content-border starts --> 
    <div id="body" class="large-8 columns" role="content">   <!-- body starts -->
    <div class="large-12 columns">   
       <form id="form1" name="form1" method="post" action="../process-quiz" >
        <?php 
            $name = 0;
            $arrlength1 = count($name);      
            echo $arrlength1;          
            for($x=0;$x<$arrlength1;$x++)
                {
                    echo "<input type='hidden' name='answer[]' value='$name[$x]' />" ;  

                }                       
               
           $p = $page->question;
          echo "<h1>Q:<span>{$p}</span></h1>";
          if($page->quiz_images !=null)
          {
              $image = $page->quiz_images->url;
              echo "<img src='{$image}'>";
          }

          foreach($page->quiz_options as $c)
            {
              echo "<input type='radio' name='answer[]' value=\"{$c->quiz_answer_text}\">{$c->quiz_answer_text}<br/>";
            }      
      
        ?>        
      <input type="submit" name="button" id="button" value="Next" />
      
     
      </form>

    </div>

    </div><!-- body ends -->

    <?php //include("./sidebar.inc"); ?>

  </div><!-- content-border ends --> 
</div><!-- content ends -->

<?php include("./footer.inc"); ?>

process-quiz.php

<?php
$name = $input->post['answer'];

$arrlength=count($name);
$myarray = array();
array_push($myarray, $name);

$answers =array('one','right');
$score=0;
foreach ($myarray as $innerArray) {
    //  Check type
    if (is_array($innerArray)){
        //  Scan through inner loop
        foreach ($innerArray as $value) {
           //echo $value."<br />";
           foreach($answers as $key)
           {
            if($value == $key)
            {
              $score++;
            } 
           }
           
        }
      }
    }
$url = "question-".$arrlength;
if($arrlength>2)
{
$url="process-quiz.php";
}

 $session->redirect("http://www.example.in/temp/quiz-section/$url",false);
 echo "<h1>you have scored :".$score."</h1>";
 echo '<pre>'; // just to check the user inputs.
 print_r($myarray);
 echo '</pre>';
}?> 
Link to comment
Share on other sites

1. Is "$name" defined in the ./functions.php file? Because in the quiz.php, you don't populate it with anything.

2. in the quiz.php, when you generate the hidden inputs, you output "$name[$x]". You should output "{$name[$x]}" (note the curly braces) to output value of $name at position $x.

That's what I could think of after quick read-through.

Link to comment
Share on other sites

 Share

×
×
  • Create New...