我无法在PHP中同时使用GET和POST [英] I can't use GET and POST at the same time in PHP

查看:155
本文介绍了我无法在PHP中同时使用GET和POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

然后我有一些从POST读取的表单检查条件:

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




 < form action =<?php echo $ _SERVER ['PHP_SELF']。?id = $ id;?> name =completemethod =post> 
< input type =submitid =textButtonname =completeSubmitvalue =[mark as complete]>
< / form>

这个页面最初通过GET方式使用一个id变量来访问:

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

所有后续表单提交(重定向到同一页面)失败。我知道你不能在同一个请求中发送GET和POST,但是看到我的表单正在提交给 $ _ SERVER ['PHP_SELF']。?id = $ id使用POST不应该起作用?这是我第一次尝试这样做,所以很可能我忽略了一些微不足道的东西。

同时使用get和post,但你不应该这样做。如果你想继续发送ID,这很简单:

 < form ... 
< ; input type =submit...
< input type =hiddenname =id
value =<?php echo htmlspecialchars($ _ GET ['id'],ENT_QUOTES );?> />
< / form>


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.

解决方案

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>

这篇关于我无法在PHP中同时使用GET和POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆