简体   繁体   English

PHP发布表单与数组索引

[英]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: HTML输入类型根据问题类型更改:

form.php 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. 但我无法通过$ _POST获取此数据。 Here is 这是

action.php 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? 我应该为我的输入命名,我应该如何通过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. 通过引用您的代码片段作为解决方案,请检查var_dump($_POST)的输出,在其输出中观察密钥qno是否存在及其对应的值。

Please try executing following code snippet to fetch all data of qno key 请尝试执行以下代码片段以获取qno密钥的所有数据

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. 乍一看,您的代码没有任何问题(因为您最近的编辑)所以您应该尝试print_r($_POST)来调试并查看您的帖子数据实际包含的内容。

Then you can iterate over the answers more easily with a foreach loop like this in action.php 然后,您可以使用action.php中的foreach循环更轻松地迭代答案

foreach ($_POST['qno'] as $i => $answer) {
    echo "Answer Number $i: $answer";
}

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 1)如上所述,调试print_r($ _ POST)的输出以验证脚本正在接收的内容

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. 2)验证生成表单的实际脚本是否没有错误,一种方法是查看正在生成的HTML代码并确保所有值都存在。

3) Simplify your code if your bug persists. 3)如果您的错误仍然存​​在,请简化您的代码。 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. 关于简化,嵌套for循环,在switch语句中,在另一个for循环中的一个额外注释并不是最好的编码实践。 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) formatQuestion($ type,$ data)

well i don't find your question with accurate data below is my sample code to help you - 好吧,我没有找到你的问题与下面的准确数据是我的示例代码,以帮助您 -

form.php 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 action.php的

<?php
echo '<pre>';
print_r($_POST);
?>

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] 如果想要所有值只是尝试qno[$j][$l]这样的唯一名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM