Is a form method=POST the same as http POST

I’m trying to figure out why $_POST doesn’t seem to hold any values when sent with http POST but it does when sent with a form method=POST…

What works…

File formPage.php has a form with method set to post and action set to …/processPage.php.

processPage.php sees the submission and properly can handle:

if( $_POST) {some actions};

If I send an http POST request to the same url (…/processPage.php) I get back a response 200 / ok and a bunch of $response info so it seems like I’m getting to the url destination ok, but nothing seems to happen at all as far as processing script. The if($_POST) fails and I can even try to set a super global variable right at the very front of the processPage.php script but it doesn’t get set with the http POST.

So is submitting the form with method=POST the same as sending an http POST method?

Is there something I need to set up on the receiving url to actually accept the data sent with the http POST (e.g. a webhook??).

As far as I know, the two should be the same, as long as you’ve got everything correct in the code that sends the post.

Yes, that’s my question. How are you sending the “HTTP POST” when not with the browser from a page with a <form method="POST"> ?

To submit the http POST I’m composing it with

$response = wp_remote_post( 'https://testdomain.com/processPage.php/', $postSetup);
// $postSetup has the value pair and option settings

And I think I have it partially working now. I’m getting the response 200 / ok and I see that is is now entering the script execution. So, the basic http POST seems to be working…
… but, I’m still having a problem, albeit a slightly different symptom.

I’m not seeing changes made to the $_SESSION global.

  1. In the calling script I set $_SESSION[‘status’] = ‘Starting…’
  2. I perform an http POST to …/processPage.php
  3. In …/processPage.php I set $_SESSION[‘status’] = ‘Finished.’
  4. I can echo $_SESSION[‘status’] and I see in the response body that it is set to ‘Finished’.
  5. In the calling script I inspect $_SESSION[‘status’] but I see that it is still set to ‘Starting…’

I thought $_SESSION was a super-global but apparently on http POST results it doesn’t get modified. Is there something special I have to do so that $_SESSION is modified and returned?

Super globals are variable that can be accessed from anywhere within your script without having to pass it around explicly. It does not mean that the value will be shared between all requests.

In the case of, $_SESSION it will be shared between requests that have the same session cookie, but the scenario you want isn’t possible as the value isn’t updated half way during a request when another request has change the value, they are only set once when session_start is called.

1 Like

Ah, thanks @rpkamp, that explains why I don’t see $_SESSION updated after the http POST. So now I’m thinking that either through the http POST or GET method I need to get an array returned and then in the originating script I can assign that array into $_SESSION for continued processing in that script.

I’m now stuck on how do I return an array to the calling script.

If I use http POST method and then in the called script I print_r the array, then I see the results in the [body] of response. But its just a printed string while what I want is an array.

If I use http GET method is there anyway to do something like return $myarray; and then be able to parse that in the calling script.

You could use something like JSON encoding, that’s often used to transfer data structures around where the transfer protocol might not support it.

2 Likes

No. When posting a form the browser also sends a specific header that tells PHP to read the data into $_POST.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.