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:
- Revert all the changes we made to
/pictures.php: Remove themethod="post"attribute, and go back to using query parameters and$_GET. - Add another form to
/random.php, which also submits its data as aPOSTrequest 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 ...