Search⌘ K

Submitting Data via the Request Body: Reviving Functionality

Explore how to replace GET with POST requests for form submissions in PHP, create hidden form fields to pass data securely, and set the form action attribute properly. Understand the implications of using POST requests, including browser behavior on page refresh. This lesson prepares you to handle form data more effectively in PHP applications.

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