2

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.

  • 8
    This should inherently work -- you might want to investigate other areas for possible bugs.. – jlb Oct 31 '11 at 14:10
  • Yes, this should work. GET and POST at the same time are indeed possible. You can try checking your form's HTML to see if the action attribute is correct. Dumping both $_GET and $_POST vars. in your target page might also be helpful. – jeanreis Oct 31 '11 at 14:15
  • typically the ID and NAME attributes should be equal... – Fosco Oct 31 '11 at 14:16
  • @jeanreis sorry for disappointing you, but GET and POST at the same time are quite impossible. – Your Common Sense Oct 31 '11 at 14:30
  • @jlb not that inherently. Say, it won't work for the GET method forms – Your Common Sense Oct 31 '11 at 14:31
  • 1
    @tandu's answer is best here -- while it's true that you cant both HTTP GET and HTTP POST at the same time, $_GET variables should be accessible from a POST request – jlb Oct 31 '11 at 14:34
  • @Col. Shrapnel quite right, I didn't explain myself correctly (jlb's answer is better). HTTP POST with GET parameters allows you to access both of them ($_GET and $_POST). – jeanreis Oct 31 '11 at 14:55

2 Answers2

6

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>
Fosco
  • 38,138
  • 7
  • 87
  • 101
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • +1 for this solution, just send the initial value of $_GET['id'] along with your post request. It's the cleanest way. – Oldskool Oct 31 '11 at 14:15
  • I tried this too and it didn't seem to work. I'll keep on investigating, thanks for the tip. –  Oct 31 '11 at 14:26
  • Got it to work, turns out there was a little bug hiding around. I opted for this method as it is cleaner. –  Oct 31 '11 at 14:38
  • I know it's a really old post, but just in case: WHY one shouldn't use post and get at the same time? Is something particularly wrong about it I should be aware of? Or is it just considered 'messy'? My situation: i'm receiving a POST generated externally (payment transaction result), and can only program where to receive it. So I put a parameter in the URL to add my own data to it. – Henry Nov 03 '13 at 21:58
0

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?
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345