How to Submit a Form in PHP

Learn how to submit HTML forms using PHP with this step-by-step guide. Discover different methods of form submission in PHP, including GET and POST requests, and explore built-in PHP functions for processing form data. Improve your web development skills and build more dynamic and interactive web applications with PHP form submission techniques.

Knowing how to manipulate forms in PHP it’s essentially the alpha and the omega in terms of data management and flow. If you cannot collect data from the user and send it from point A to point B, your app can be as good as a calculator. Sending or submitting form data capabilities to a specific script for handling can be found to a very basic login form up to a asynchronous API request that updates an active record of a service somewhere on the cloud. So what are those steps of submitting a form?

Creating the form elements

Before we can try to submit data to a script, it is crucial to have some way to get the data from the user. This can be done using form elements with HTML. There are many elements for different use cases but the ones that are commonly used are the text and number inputs alongside a submit button. Suppose we want the users to submit the electrical measurements of their houses. The form will contain at least four elements (firstname, lastname, day_measurement, night_measurement). All the data collected will be submitted (forwarded) to a script called calculate.

<style>input { display: block; margin-bottom: 5px; }</style>
<form action="calculate.php" method="get">
<input type="text" name="firstname" placeholder="e.g: John">
<input type="text" name="lastname" placeholder="e.g: Doe">
<input type="number" name="day_measurement" placeholder="e.g: 49302">
<input type="number" name="night_measurement" placeholder="e.g: 12093">
<button type="submit">
<span>Send Data</span>
</button>
</form>

As you can see there is nothing fancy happening here. We have the four elements represented as form fields, the submit button and some basic styling for the input elements. The important points to pay attention to is the form’s attributes action which where we define the script that it’s going to handle the submit and the method field which designates the corresponding HTTP method that’s going to be used while passing the data. The are some key differences between GET and POST.

If you choose to use GET as the designated method, the data will be passed via the URL which means, that all the data that the user submitted will be visible to the naked eye. So keep in mind that you need to make sure if the data that you want to get from the user is sensitive or not, otherwise the data will be exposed like lights on a Christmas tree.

When you know that the user will enter sensitive information to your form, much like usernames and passwords to a service, you are obliged to use POST as the designated method to protect the data from the naked eye and the potential attackers.

Submitting and Handling the data

Now that we have created the frontend portion of the example, it’s time to handle the data from the backend side and display the corresponding template depending the use case and condition we choose.

<style>td { padding: .5em; border: 1px solid #eee; }</style>
<?php
$_first = $_GET['firstname'];
$_last = $_GET['lastname'];
$_daym = $_GET['day_measurement'];
$_nightm = $_GET['night_measurement'];
if ($_first != '' && $_last != ''): ?>
<div style="width: 300px; margin: 2em auto;">
<table border="1">
<tr>
<td>Day Consumption</td>
<td><?= $_daym ?></td>
</tr><tr>
<td>Night Consumption</td>
<td><?= $_nightm ?></td>
</tr>
</table>
</div>
<?php else: ?>
<div style="width: 300px; margin: 2em auto;">
<p>There's been an error with the submission. Try again <a href="index.php">here</a>.</p>
</div>
<?php endif ?>

When storing the variables fetched from the super global $_GET or $_POST, I like to add an underscore between the dollar sign and the name of the variable. This makes the variable quite handy when you try to concatenate it within a SQL query string. In SQL, when the name of the variable’s the same with the field of the table that you try to change, there are some unintended consequences. The query becomes malformed and you may not get the proper result that you’re looking for. Afterwards, it’s just a matter of what you check as deciding conditions to render the template of the consumptions or the error message that will guide the user to the form. If you want to use the POST method, you just replace everything with $_POST instead of $_GET to the calculate script, the method=”get” to method=”post” to the form and everything’s fine and dandy. Feel free to watch the video at the end for a more detailed coding example.