开发者

getting the value posted in the form in the array

I am making and computerized examination system in which a student gets 50 questions and three choices for each question. And I have displayed the question in tabs in which each tab contains 10 questions. I have made the form but confused to get the posted value.

Here is my code:

<div id="page-wrap">

    <div id="tabs">

    <ul>
        <li><a href="#fragment-1">Questions 1-10</a></li>
        <li><a href="#fragment-2">Questions 11-20</a></li>
        <li><a href="#fragment-3">Questions 21-30</a></li>
        <li><a href="#fragment-4">Questions 31-40</a></li>
        <li><a href="#fragment-5">Questions 41-50</a></li>
   </ul>
<form method="post" action="result.php" >
    <?php
    $data = $objQuestion_set->selectDetail($exam_id, $registration_id);
        $objQuestion_bank = new Question_bank();
        ?>

  <div id="fragment-1" class="ui-tabs-panel ui-tabs-hide">
        <?php
                $value[] = $data['question_1'];
                $value[] = $data['question_2'];
                $value[] = $d开发者_运维技巧ata['question_3'];
                $value[] = $data['question_4'];
                $value[] = $data['question_5'];
                $value[] = $data['question_6'];
                $value[] = $data['question_7'];
                $value[] = $data['question_8'];
                $value[] = $data['question_9'];
                $value[] = $data['question_10'];
                for($i = 1; $i <=10; $i++)
                    {
                        $question = $objQuestion_bank->selectDetail($value[$i-1]);

                        echo "<div><p>".$i . '.' . $question['question']."</p>";
                        echo "<br />";
            ?>
                    <input type="radio" id="answer_1_2" name="answer_<?php echo $i;?>"  value="<?php echo $question['answer_1'];?>" /><label for="answer_1"><?php echo $question['answer_1'];?></label>
                    <input type="radio" id="answer_1_2" name="answer_<?php echo $i;?>"  value="<?php echo $question['answer_2'];?>" /><label for="answer_1"><?php echo $question['answer_2'];?></label>
                <input type="radio" id="answer_1_2" name="answer_<?php echo $i;?>"  value="<?php echo $question['answer_3'];?>" /><label for="answer_1"><?php echo $question['answer_3'];?></label>
                <input type="hidden" id="question_id_<?php echo $i;?>" name="question_id_<?php echo $i;?>" value="<?php echo $question['question_id']; ?>"class="hidden_element" />
        <?php
                            echo "</div>";
                            echo "<br /><br />";
                    }

            ?>

    </div>

Now i want to get the posted value of question_id which is hidden field and print it.


You haven't really given much information, and there are some errors in your posted code, so I'm going to answer this generally and leave it to you to try to integrate it.

You are assigning essentially useless names to your form values that make it incredibly complicated to retrieve values in a meaningful way. PHP's argument parsing provides useful abilities to parse arguments into arrays, so I would recommend using them.

For instance, if you had the following input boxes and names in your form, the POSTed data would be very straightforward.

<input type="radio" name="values[<?php print $i; ?>][answer]" value="1">
<input type="radio" name="values[<?php print $i; ?>][answer]" value="2">
<input type="radio" name="values[<?php print $i; ?>][answer]" value="3">
<input type="radio" name="values[<?php print $i; ?>][answer]" value="4">
<input type="hidden" name="values[<?php print $i; ?>][question]" value="10">

Or you could just have this if the $i values isn't important.

<input type="radio" name="values[][answer]" value="1">
<input type="radio" name="values[][answer]" value="2">
<input type="radio" name="values[][answer]" value="3">
<input type="radio" name="values[][answer]" value="4">
<input type="hidden" name="values[][question]" value="10">

Both of these would make your $_POST array into a nice nested array like the following.

<?php
$_POST['values'] == array(
  array(
    'answer' => 3,
    'question' => 1
  ),
  array(
    'answer' => 2,
    'question' => 2
  )
)

You haven't actually shown how you are processing the submitted values, so I can't really give more information, but this is the best way to structure form data for easy processing.

This page has some very basic examples. http://www.askaboutphp.com/23/parsing-url-queryingstring.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜