D.Joe D.Joe - 1 year ago 73
PHP Question

PHP posting form with array indices

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


$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];
<form action="action.php" method="post" name="form">
for ($j = 0; $j < $i; $j++) {
switch ($qtype[$j]) {

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

case checkbox:

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


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



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?

Answer Source

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";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download