提交带有ID变量的表单

时间:2020-04-05 16:26:52

标签: php

我在while循环中有几种这样的表单,如果我单击一个按钮,那么页面上的所有表单都将提交,因此我为表单id="formid-<?php echo $i; ?>"分配了一个变量,它在每个循环中都会改变,以及如何在何时定位变量提交表格?

<?php 

$in = 0;

while (mysqli_stmt_fetch($stmt)):   

    if(isset($_POST['send'])){
        $sub = $subscript + 1;
        $stmt = mysqli_prepare($conn, "UPDATE users 
                                        SET subscript = ? 
                                        WHERE usernic = ? ");
        mysqli_stmt_bind_param($stmt, 'is', $sub, $my_username);
        mysqli_stmt_execute($stmt);
    }   
?>

<form action="" method="post" id="formid-<?php echo $i; ?>"> 
    <h6  class="badge badge-warning font top-8">
        <input type="submit" name="send" value="Subscribe" id="send">
        <span class="badge badge-light">

    <?php 
         echo $subscript; 
    ?>
        </span>
        <span class="sr-only">Subscribe</span>
    </h6>
</form>

<?php  $in++;    endwhile;  mysqli_stmt_close($stmt); } ?>

1 个答案:

答案 0 :(得分:0)

我找到了解决方法:

首先,在while循环之前,我具有以下条件:

$i = 0;

在循环结束之前,我有增量:

$i++;

然后我在表单中放置了一个隐藏的输入:

<input type="hidden" name="formname" value="<?php echo $i; ?>">

然后我在下面加上所有代码:

if(isset($_POST['send'])){

与此有关,如果:

if ( !empty($_POST['formname']) && ( $_POST['formname'] == $i ) || ( $_POST['formname'] == 0 ) ){ }

这样我就可以定位所需的表格

相关问题