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

妖精的绣舞 提交于 2019-11-28 10:22:01

问题


Near the top of my page, I have this:

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

Then I have some form check conditionals that read from POST:

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

And finally, I have an HTML form which looks like this:

<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:

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? This is my first time trying this so it is quite possible I've overlooked something trivial.


回答1:


You can use get and post at the same time, but you shouldn't. If you want to continue to send the ID this is as simple as:

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



回答2:


Of course you can not use GET and POST methods simultaneously.

However you can use a query string while sending a form using POST method, which being used to populate $_GET array.

To find a certain error you have to provide more info. At least 2 things:

  • how does HTML form look
  • what do yo see in the query string after posting the form.

and errr...

  • do you use any header redirects in the form processing?


来源:https://stackoverflow.com/questions/7954831/i-cant-use-get-and-post-at-the-same-time-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!