...

/

Submitting Data via the Request Body: Reviving Functionality

Submitting Data via the Request Body: Reviving Functionality

Let's restore the functionality of our application as it became broken after switching the form method from GET to POST.

We'll cover the following...

Replacing GET with POST

Instead of $_GET['picture'] and $_GET['number'], we should use $_POST['picture'] and $_POST['number']. If you replace every occurrence of $_GET with $_POST inside pictures.php, everything should work again.

Unfortunately, using $_POST instead of $_GET breaks the integration with /random.php. As you may remember, it has a link to /pictures.php, and this link still adds a query parameter for the number of pictures to show. We can do two things:

  1. Revert all the changes we made to /pictures.php: Remove the method="post" attribute, and go back to using query parameters and $_GET.
  2. Add another form to /random.php, which also submits its data as a POST request with a request body.

Option 1 is easy, but option 2 will be more interesting, so let’s do that.

Adding a form with a hidden field on random.php

On ...