Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找php循环中生成的变量_Php_Html - Fatal编程技术网

查找php循环中生成的变量

查找php循环中生成的变量,php,html,Php,Html,我有以下php代码: for($i=0;$i<3;$i++) { echo 'Question'.$i.'</br>'; echo 'Answer..</br>'; echo '<form action="" method="POST">'; echo '<textarea name="answer"></textarea>'; echo '</br><button nam

我有以下php代码:

for($i=0;$i<3;$i++) {
    echo 'Question'.$i.'</br>';
    echo 'Answer..</br>';
    echo '<form action="" method="POST">';
    echo '<textarea name="answer"></textarea>';
    echo '</br><button name="answer_button'.$i.'"><b>Answer</b></button>';
    echo '</form>';
}
这会给我问题编号,但一旦单击按钮,它不会在循环中打印其他问题


没有javaScript就没有解决方案吗

将html标记更改为使用“数组表示法”:

echo'Answer';
这将导致php在收到可检查的表单时填充数组:

<?php
// ...
if(isset($_POST['answer_button']) && is_array($_POST['answer_button'])) {
    $id = array_shift(array_keys($_POST['answer_button']));
    // ...
}

将html标记更改为使用“数组表示法”:

echo'Answer';
这将导致php在收到可检查的表单时填充数组:

<?php
// ...
if(isset($_POST['answer_button']) && is_array($_POST['answer_button'])) {
    $id = array_shift(array_keys($_POST['answer_button']));
    // ...
}
这应该可以:

for($i=0;$i<3;$i++) {   
    echo 'Question'.$i.'</br>';
    echo 'Answer..</br>';
    echo '<form action="" method="POST">';
    echo '<textarea name="answer"></textarea>';
    echo '</br><button name="answer_button'.$i.'"><b>Answer</b></button>';
    echo '</form>';
    echo '</div>';
}

for($i=0;$i<3;$i++) {
    if(isset($_POST["answer_button".$i])) {
        echo $i;
    }
}
对于($i=0;$i这应该可以:

for($i=0;$i<3;$i++) {   
    echo 'Question'.$i.'</br>';
    echo 'Answer..</br>';
    echo '<form action="" method="POST">';
    echo '<textarea name="answer"></textarea>';
    echo '</br><button name="answer_button'.$i.'"><b>Answer</b></button>';
    echo '</form>';
    echo '</div>';
}

for($i=0;$i<3;$i++) {
    if(isset($_POST["answer_button".$i])) {
        echo $i;
    }
}

for($i=0;$i我想这就是你想要的

代码


我想这就是你想要的

代码


它不打印其他问题。没有冒犯,但我给出的最接近的解决方案更好。看看你是否可以修改它更好。你确定吗?通过测试,它会写所有问题,最后是点击按钮索引。它不打印其他问题。没有冒犯,但我给出的最接近的解决方案更好。看看你是否你确定吗?测试了它,它写下了所有的问题,最后是点击按钮索引。
<?php
echo '<form action="" method="POST">';
for ($i = 0; $i < 3; $i++) {
    echo 'Question: ' . $i . '<br>';
    echo 'Answer..<br>';
    echo "<textarea name='answer[$i]'> </textarea></br>";
    echo "</br><button name='answer_button[$i]' value='BtnPushed'>   <b>Answer</b></button><br>";
    if (! empty($_POST['answer_button'][$i])) echo "Last Answer: {$_POST['answer'][$i]}<br>";
    echo '<hr>';
}
echo '</form>';