简体   繁体   English

我无法在PHP中同时使用GET和POST

[英]I can't use GET and POST at the same time in PHP

Near the top of my page, I have this: 在页面顶部附近,我有以下内容:

<?php $id = $_GET['id']; ?>

Then I have some form check conditionals that read from POST: 然后我有一些从POST读取的表单检查条件:

if (isset($_POST['completeSubmit'])) {
        //code
}

And finally, I have an HTML form which looks like this: 最后,我有一个HTML表单,看起来像这样:

<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"; ?>" name="complete" method="post">
<input type="submit" id="textButton" name="completeSubmit" value="[mark as complete]">
</form> 

The page is initially accessed by using GET with an id variable like this: 该页面最初是通过使用带有ID变量的GET访问的,如下所示:

http://website.com/page.php?id=1

All subsequent form submissions (which get redirected to the same page) fail. 所有后续表单提交(重定向到同一页面)均失败。 I know you can't send both GET and POST in the same request, but seeing as my form is submitting to $_SERVER['PHP_SELF']."?id=$id" using POST shouldn't it work? 我知道您无法在同一请求中同时发送GET和POST,但是看到我的表单正在使用POST提交至$_SERVER['PHP_SELF']."?id=$id"使用POST应该行不通吗? This is my first time trying this so it is quite possible I've overlooked something trivial. 这是我第一次尝试,因此很可能我忽略了一些琐碎的事情。

You can use get and post at the same time, but you shouldn't. 可以同时使用get和post,但不应使用。 If you want to continue to send the ID this is as simple as: 如果您要继续发送ID,这很简单:

<form ...
   <input type="submit" ...
   <input type="hidden" name="id"
      value="<?php echo htmlspecialchars($_GET['id'], ENT_QUOTES); ?>" />
</form>

Of course you can not use GET and POST methods simultaneously. 当然,您不能同时使用GET和POST 方法

However you can use a query string while sending a form using POST method, which being used to populate $_GET array. 但是,您可以在使用POST方法发送表单时使用查询字符串 ,该方法用于填充$ _GET数组。

To find a certain error you have to provide more info. 要查找某个错误,您必须提供更多信息。 At least 2 things: 至少有两件事:

  • how does HTML form look HTML表单外观如何
  • what do yo see in the query string after posting the form. 发布表单后,您会在查询字符串中看到什么。

and errr... 还有...

  • do you use any header redirects in the form processing? 在表单处理中是否使用任何标题重定向?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM