douhong1703 2014-05-25 14:28
浏览 30
已采纳

有没有办法检查$ _POST是否存在? [重复]

This question already has an answer here:

I am repeatedly calling the same page to perform the same action an x amount of times. In the page before this loop, I have a form asking for input. Only the first time when the loop runs I want to use the given value, but when I have the $_POST in the code, it gives an error.

So I have two questions:

  1. Does PHP go on with executing the code when 'erroring', if not, is there a way to let it do so, because that would solve my problem.
  2. Can I skip a piece of the code? Something like skip lines 12-14 or something...

I could post my code, but I don't think that will make much sense, since I have some other pages too and txt files and such and if you don't know what it is for, it is rather hard to understand.

I will summarize though:

page1.html:

<form action="page2.php" method="post">
<input type="text" name="input">
<input type="submit" value="Submit">
</form>

page2.php:

for($i = 0; $i < $_POST['text']; $i++){
print "<form action=\"page2.php\" method=\"post\">";
}

As you can see, the loop calls page2.php again, but the $_POST['text'] will not exist anymore. So, is there a way to go around this?

I have tried this:

if($another_counter_from_another_page > 1){
//Do nothing
}
else
{
$_SESSION['counter']=$_POST['text'];
}

And then replace the $_POST['text'] in the for-loop with $_SESSION['counter'], but I still get an error even when $another_counter_from_another_page > 1 is true, so that is why I asked for a way to skip a piece of code.

Thank you in advance, I hope my problem is clear from the example I gave...

</div>
  • 写回答

1条回答 默认 最新

  • duanhua5523 2014-05-25 14:33
    关注

    For a single key:

     if(isset($_POST['myKey'])) {
         echo "myKey exists"
     }
    

    If you want to know if there is any POST data:

    if(!empty($_POST)) {
        echo "We have POST data"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?