【问题标题】:Working of button click inside loops, while or foreach在循环、while 或 foreach 内单击按钮的工作
【发布时间】:2020-04-18 08:34:15
【问题描述】:
<form action="" method="post">
<input type="submit" name="sb">
<?php
    $i=0;
    while($i<10) {  
        if(isset($_REQUEST['sb'])) {
            echo $i;
        }
        $i++;
    }
?>
</form>

点击按钮时我得到0 1 2 3 4 5 6 7 8 9

我希望9 是结果,但0 to 9 怎么样?

当内部有循环时,这个点击事件如何处理?

【问题讨论】:

    标签: php forms loops while-loop


    【解决方案1】:

    根据您的代码,在按下按钮之前和按下按钮之后这两种情况下都会执行 while 循环,并且您已经在 while 循环中打印了 i,这就是您得到 0 的原因结果是 em> 到 9

    试试下面的代码。

    <form action="" method="post">
    <input type="submit" name="sb">
    <?php
    $i=0;     
    if(isset($_REQUEST['sb']))
    {
        while ($i<10)
        {
            $i++;
        }
        echo $i;
    }
    
    ?>
    </form>
    

    【讨论】:

    • 请在您的答案中添加一些解释,以便其他人可以从中学习
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多