问题描述
我遇到了使用以下脚本的
I have come across scripts that use:
isset($_POST['submit'])
以及使用以下代码的代码:
as well as code that uses:
$_SERVER['REQUEST_METHOD']=='POST'
我想知道两者之间的区别以及哪种方法最好.
I was wondering the difference between these two and which method is best.
推荐答案
这意味着两件事.首先,检查是否在提交表单时传递了参数submit
.许多人使用此代码片段来验证是否已发送表单.之所以可行,是因为提交按钮在技术上是一个<input>
,因此它的值会与表单中的其他任何元素一起发送.
These mean two different things. The first, checks to see if when the form was submitted the parameter submit
was passed. Many use this snippet to verify that a form has been sent. This works because the submit button is technically an <input>
so it's value is sent along with any other elements that were part of the form.
<?php
if(isset($_POST['submit'])) { // This way form and form logic can be adjacent to each other
// Logic
}
?>
<form method='POST' action='<?= $_SERVER['REQUEST_URI'] ?>'>
<!--- other form stuff -->
<input type="submit" name="submit" value="Send!" />
</form>
第二个片段测试表单是否使用POST方法提交.这并不一定意味着按下了表单按钮.如果未通过POST提交,那么超全局$_POST
将为空.
The second snippet tests if the form was submitted with the POST method. This doesn't necessarily mean that the form button was pushed. If it wasn't submitted with POST, then the superglobal $_POST
would be empty.
这篇关于isset($ _ POST ['submit'])vs $ _SERVER ['REQUEST_METHOD'] =='POST'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!