We mainly use HTML forms when collecting user input in web-based applications. They range from contact forms, login forms, and also registration forms. Forms are the fundamental interface between the user and the server. Creating a form on a web application is achieved using HTML. PHP connects the web application with the database server. Show
Through this article, you will learn how to:
PrerequisitesBefore we begin you should have prior knowledge of HTML, PHP, and MySQL. Overview of formsA form is a document with blank fields for a user to insert or update data. The user’s data is stored in the database and retrieved anytime. Using forms to collect data in a web application is a simple task. Some of the popular forms include:
The form is presented to the user who inserts data and submits the form using a Hypertext Transfer Protocol (HTTP) enables communication between the client (browser) and the server. An HTTP request is sent by a client to the server which then returns a response. Though HTTP supports several methods, we will focus on The The PUT method is How to create HTML formsHTML forms can contain special elements such as buttons, radio buttons, and checkboxes. It, therefore, becomes easy for the user to interact with the webpage. Forms should be user-friendly. This means that a user with no technical skills should be able to use it. Forms are defined by the Here is an example of a form that submits data to a file named
The output for the above code is as shown in the screenshot below. The POST method
The POST method is preferred because data sent through it is not visible in the URL. The POST method is also important because data cannot be decoded by looking into web server logs. POST does not have a limitation on the amount of data sent from the form. This is because data is submitted via the body of the HTTP request. The Processing the form data (PHP script)The PHP code below can be used to process input from an HTML form with the POST method. The code should be put in the script, which is the form target. It can be in the same script where the HTML form is, or it can be on a different script. In this case, we will have the PHP code in the same script as the HTML form.
The output of the code above is as shown in the animation below. GET method
Data transferred via this method is visible on the URL of the HTTP request. The HTTP request can be cached and saved in the browser history. The disadvantage of the GET method is that it should not be used with sensitive data such as passwords because it is not secure. The GET method has a limitation of the amount of data sent from the form. The data being sent on the URL depends on the web server’s operating system and the type of browser. Most systems have a limit of
Here is the output of the GET method example. Tabular comparison of GET and POST methods
HTML forms and MySQL database CRUD operationsWe will learn how to perform CRUD operations on a MySQL database using HTML forms. We will learn how to use HTML forms to create, read, update and delete data from the MySQL database. First, create a MySQL database and name it The columns are:
Create a database server connectionAfter creating the database and tables, we need a PHP script that connects to the MySQL database server. Create a file named
We will later include this file using the CreateTo create a record in the database, use the code below.
Once the form is submitted via the
Here is the form.
The video below shows how the code above works.
ReadThe code below retrieves the inserted data and displays it in an HTML table.
Below is an animation of how the read operation works. UpdateHTML forms are used to update existing data in the database. In this case, we will implement the update functionality. The Take note of the code used to create the edit
button on the table. The Once the
In the The script below is used to update the data.
The video below shows how the update function will work on the browser. DeleteTo delete a record in the table, the user clicks
the
The
The delete functionality works as shown in the video below. You can download the source code from here. ConclusionIn conclusion, a HTTP request enables communication between a client and a server. We have learned how to create HTML forms and process the data using PHP. We have also learned about the POST, PUT and GET methods. We learned that I hope this article will shed some light and give you an understanding while working with HTML forms in PHP. Peer Review Contributions by: Wanja Mike Which method is used to pass data from HTML to PHP?PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post".
How do you input in PHP?To get input from users, you also have to prompt them to enter something. You can use PHP's `readline() function to get this input from the console.
How can I get HTML value from PHP?Use PHP's $_POST or $_GET superglobals to retrieve the value of the input tag via the name of the HTML tag.
Can PHP take user input?PHP is a server side language and can NOT pop up a window on a client machine. You need to use client-side Javascript. Use PHP CLI. You won't get a fancy popup, but it can get user input from the command line.
|