防止表单中的重复提交按钮

时间:2014-08-17 21:24:22

标签: php forms submit

我正在生成一个包含存储在数据库中的所有问题的表单。我不明白我怎么能阻止提交按钮从每一行(问题)复制自己?我只想在表单的末尾有一个提交按钮。

// generate all questions
$query = "SELECT * FROM questions";
$result = @mysqli_query($con, $query);

if ($result) {
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

    $body = $row['question_body'];
    $question_id = $row['question_id'];

    echo '  
        <tr>
            <form action="insert.php" method="POST">
                <td><input type="hidden" name="question_id" value="'.$question_id.'">'.$question_id, $body.'</td>
                <td><input type="radio" name="answer_value" value="1"></td>
                <td><input type="radio" name="answer_value" value="2"></td>
                <td><input type="radio" name="answer_value" value="3"></td>
                <input type="submit">
         </tr>
             </form>
                        <br/>';
}

2 个答案:

答案 0 :(得分:1)

您需要在while循环之外移动提交按钮。 (作为旁注,我更喜欢alternative syntax for control structures。)

<?
// Generate all questions
$query = "SELECT * FROM questions";
$result = @mysqli_query($con, $query);
if ($result) :
?>
<form action="insert.php" method="POST">
    <table>
<?
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) :
        $body = $row['question_body'];
        $question_id = $row['question_id'];
?>
        <tr>
            <td><input type="hidden" name="question_id" value="<? echo $question_id ?>"><? echo $question_body ?></td>
            <td><input type="radio" name="answer_value" value="1"></td>
            <td><input type="radio" name="answer_value" value="2"></td>
            <td><input type="radio" name="answer_value" value="3"></td>
        </tr>
<?
    endwhile;
?>
        <tr>
            <input type="submit">
        </tr>
    </table>
</form>
<?
endif;
?>

答案 1 :(得分:0)

您将表单的开始和结束标记以及提交按钮放在表格外(以及while循环)。