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?
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];
}
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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With