php 如果 (!empty($_POST)) 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12528524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
if (!empty($_POST)) not working
提问by Mark Adams
I have a php form (code to follow) with a submit button that runs JSON-events.phpas its action(method = POST). In the JSON-events code I am testing whether the form has been submitted using if (!empty($_POST)). My issue is that the JSON-events code doesnt seem to be recognising $_POST.
我有一个 php 表单(要遵循的代码),其中有一个JSON-events.php作为action( method = POST)运行的提交按钮。在 JSON 事件代码中,我正在测试表单是否已使用if (!empty($_POST)). 我的问题是 JSON 事件代码似乎无法识别$_POST.
Heres the form side code section.
这是表单端代码部分。
<div class="simple_overlay" id="searchform">
<form id="myform" class = "cols" action="json-events.php" method="post" >
<h3>Search</h3>
<p>
<label>address/postcode *</label>
<input type="address" id="searchaddress" />
</p>
<p>
<label for="amount">maximum distance:</label>
<input type="text" id="amount" value="5 miles" style=" border:0; color:#43a8c4; font-weight:bold;" />
</p>
<div id="slider-range-min" style="width:130px; margin-left:4px; margin-bottom:20px"></div>
<p id="terms">
<label>Category 1</label>
<input name="cat1" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 2</label>
<input name="cat2" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 3</label>
<input name="cat3" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 4</label>
<input name="cat4" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 5</label>
<input name="cat5" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 6</label>
<input name="cat6" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 7</label>
<input name="cat7" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 8</label>
<input name="cat8" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 9</label>
<input name="cat9" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 10</label>
<input name="cat10" type="checkbox" value="1" checked="checked"/>
</p>
<p>
<input type="hidden" name="searchlat" id="searchlat"/>
</p>
<p>
<input type="hidden" name="searchlong" id="searchlong" />
</p>
<input type="submit" name="search" id="search"/>
<input type="button" value="Check Address" onclick="codeAddressSearch()"/>
<button type="reset">Reset</button>
</form>
</div>
and here is the top part of the JSON...
这是JSON的顶部...
if (!empty($_POST)) {
any help very much appreciated
非常感谢任何帮助
回答by Vijay Joseph
empty()will work if $_POSTdoes not have any values (Empty array), but in your case when you submit without any values still you get an array like below and it's not empty:
empty()如果$_POST没有任何值(Empty array),它将起作用,但是在您的情况下,当您在没有任何值的情况下提交时,您仍然会得到一个如下所示的数组并且它不是空的:
Array
(
[searchlat] =>
[searchlong] =>
[search] => Submit Query
)
empty()will return true only if $_POSTreturns
empty()仅在$_POST返回时才返回 true
Array (
)
But it's not going to happen as every form will have one Sumbitbutton.
但这不会发生,因为每个表单都会有一个Sumbit按钮。
Just use
只需使用
if($_POST) {
//php code
}
It will solve your problem.
它会解决你的问题。
Learn more about empty() by visiting http://php.net/manual/en/function.empty.php
通过访问http://php.net/manual/en/function.empty.php了解有关 empty() 的更多信息
回答by JvdBerg
Do not use
不使用
if (!empty($_POST)) {
to check if there is a post done use this:
要检查是否有帖子完成,请使用:
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
回答by Alaa Gamal
Conditions that you can use with $_POST
可以与 $_POST 一起使用的条件
if(count($_POST)>0){
//...
}
if(isset($_POST['field_name'])){
//...
}
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
//..
}
回答by Harinder
Try this
尝试这个
if(!empty($_POST['search']) && isset($_POST['search']))
回答by user410932
much better to check for an actual value in the $_POST array. Eg.
检查 $_POST 数组中的实际值要好得多。例如。
if (!empty($_POST['search']))
回答by Chirag
Using empty() will fail if value 0 is posted
So it is better to useisset($_POST['FIELD_NAME'])
如果发布了值 0,则使用 empty() 将失败所以最好使用isset($_POST['FIELD_NAME'])

