How to fill all input fields automatically from database by entering input in one textbox using PHP ?
XAMPP is a free and open-source cross-platform web server solution developed by Apache Friends, consisting mainly of the Apache HTTP Server, MySQL database, and interpreter for scripts written in the PHP programming language. The XAMPP server helps to start Apache and MySQL and connect them with the PHP file.
Approach: We need to create a MySQL database in our localhost server using the phpMyAdmin tool. We create an HTML form containing input fields that are linked with PHP code. PHP is used to connect with the localhost server and fetch the data from the database table by executing the MySQL queries. Refer to the GFG article: PHP program to fetch data from localhost server database using XAMPP
Consider, we have a database named gfg, a table named userdata. To fetch data from the database without page reload or submit for other input fields corresponding to the user entry of one field is implemented below. The data fetched is displayed in the same HTML form.
How does it work?
The XMLHttpRequest object can be used to request data from a web server, update a web page without reloading the page. You can fire call on the key up, key down, or on blur of the first text box.
Example: Suppose you enter the user_id in the respective input field. If the user_id is present in the database then the XMLHttpRequest object gets the value of the first_name and last_name corresponding to that user_id from the database. It replaces the innerHtml of the other input textboxes with the data you get in the response.
Steps for execution:
Create your HTML webpage
2. Get the requested user_id , execute the MySQL query to fetch the corresponding data for that user_id from the database table and print the data in JSON form back to the server in gfg.php file.
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.