3

I am generating a survey questions page, with questions from database. HTML input type changes in accordance with question type:

form.php

<?php
$query = "select q_id,qtext from questions order by q_id ";
$result = mysqli_query($conn, $query); // replaced with procedural mysqli
if (mysqli_num_rows($result) == 0)
    $flag = 1;
else {
    if (!$result)
        $result_list = array();
    while ($row = mysqli_fetch_array($result)) {
        $result_list[] = $row;
    }
    $i = 0;
    foreach ($result_list as $row) {
        $q_id[$i] = $row[0];
        $qtext[$i] = $row[1];
        $i++;
    }
}
?>
<form action="action.php" method="post" name="form">
    <?php
    for ($j = 0; $j < $i; $j++) {
        unset($res_list);
        switch ($qtype[$j]) {

            case text:
                echo " <textarea name='qno[$j]'></textarea><br/>";
                break;

            case checkbox:

                for ($l = 0; $l < 3; $l++)
                    echo "<input type='checkbox'  name='qno[$j]' > <label>  $l </label>";
                break;
        }
    }
    ?>

</form>

This page is working fine. But I can't get this data via $_POST. Here is

action.php

<?php

for ($j = 0; $j <= $no_of_ques; $j++) {
    $answer[$j] = $_POST['qno'][$j];
    echo $answer[$j];
}
?>

What name should i give to my inputs and how should I get them via POST?

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
D.Joe
  • 51
  • 5

5 Answers5

2

Through referring your code snippet as a solution, Please examine the output of var_dump($_POST), within its output observe that key qno exists with its corresponding values.

Please try executing following code snippet to fetch all data of qno key

for($i =0 ; $i < count($_POST['qno']) ;$i++) {
    echo $_POST['qno'][$i];
}
Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26
Nikunj K.
  • 8,779
  • 4
  • 43
  • 53
1

At first glance, there's nothing wrong with your code (since your most recent edits) so you should try print_r($_POST) to debug and see what your post data actually contains.

Then you can iterate over the answers more easily with a foreach loop like this in action.php

foreach ($_POST['qno'] as $i => $answer) {
    echo "Answer Number $i: $answer";
}
  • There is something wrong, in the second case the "name" attribute is wrong (single quote not in place and double equal sign) – Ron Dadon May 24 '16 at 05:50
  • print_r($_POST) gives output as 'array()'. Also, its warning me that an invalid argument is supplied for foreach(). Do I need to do something with $i before this? – D.Joe May 24 '16 at 05:50
  • If array() is the output then your form isn't sending any data. And of course you can't iterate over `$_POST['qno']` if it doesn't exist, hence the warning. – But those new buttons though.. May 24 '16 at 05:56
  • Maybe you can post the actual html source as it is rendered. – But those new buttons though.. May 24 '16 at 05:58
  • It works now! Though when I have a radio group as input, { for($l=0;$l<$k;$l++) { echo " "; } } I can't figure what modification in the foreach iteration will get me all the inputs. Thanks for the help though! – D.Joe May 24 '16 at 07:20
  • Checkboxes only send data i they are checked. For those, you need to either explicitly check if the question number is set, or do something with javascript to populate a hidden input like described here: http://stackoverflow.com/questions/1809494/post-the-checkboxes-that-are-unchecked – But those new buttons though.. May 24 '16 at 13:50
1

Three pieces of advice I have to solve your problem:

1) As mentioned, debugging the output of print_r($_POST) to verify what the script is receiving

2) Verify the actual script generating the form is error free, one way to do this would be to look at the HTML code that's being generated and make sure all the values are there.

3) Simplify your code if your bug persists. Save a copy of what you have, and keep removing extra stuff until it starts working as expected. I think of it as reverse engineering or more simply, just taking a few steps backwards to find out where something went wrong.

One extra note about simplifying, nesting a for loop, inside a switch statement, inside another for loop is not the greatest coding practice. There is definitely a simpler way to get questions out of a database and then dumped into a form.

One suggestion for an alternative would be to have a helper function format the questions, once given the type of question and the data. eg. formatQuestion($type, $data)

Sami Fouad
  • 376
  • 2
  • 9
1

well i don't find your question with accurate data below is my sample code to help you -

form.php

<?php
$result_list = array(0 => 'QuestionId', 1 => 'QuestionText');
$q_id = $result_list[0];
$qtext = $result_list[1];
?>

<form action="action.php" method="post" name="form">
    <?php
    $qtype = array(0 => 'text', 1 => 'checkbox');
    $i = 2;
    for ($j = 0; $j < $i; $j++) {
        unset($res_list);
        switch ($qtype[$j]) {
            case text:
                echo " <textarea name='qno[$j]'></textarea><br/>";
                break;

            case checkbox:

                for ($l = 0; $l < 3; $l++)
                    echo "<input type='checkbox'  name='qno[$j]' > <label>  $l </label>";
                break;
        }
    }
    ?>
    <input type="submit" value="submit">
</form>

action.php

<?php
echo '<pre>';
print_r($_POST);
?>
rocky
  • 631
  • 5
  • 14
0

I think this loop has a problem

for ($l = 0; $l < 3; $l++)
                    echo "<input type='checkbox'  name='='qno[$j]' > <label>  $l </label>";

It give always same name for all three elements

If want all values just try unique names like qno[$j][$l]

vijaykumar
  • 4,658
  • 6
  • 37
  • 54