Q:
How can I post a form without refreshing the page?
belongs to collection: PHP Programming Exercises
PHP Programming Exercises
- Write a php program to compare between things that are not integers
- Write a division table program in PHP using for loop
- Write a program in PHP to print prime numbers between 1 and 100
- Write a php program to print numbers from 10 to 1 using the recursion function
- Write a php program to store the username in cookie and check whether the user have successfully login or not
- Write a php program to convert the given string into an array
- Write a php program to loop over the json data
- Write a program in PHP to remove all html tags except paragraph and italics tags
- Write a program to loop through an associative array using foreach() or with each()
- Write a php program to differentiate between fgets, fgetss and fgetcsv
- There are two deals of an item to buy. The quantities and prices of the item are given below. Write a program in PHP to find the best deal to purchase the item
- Write a php program to set session on successful login
- Write a program in PHP to read from directory
- PHP create image from text and save
- How to get data from XML file in PHP
- PHP Create Word Document from HTML
- How to check whether a year is a leap year or not in PHP
- Fibonacci Series Program in PHP
- How to generate QR Code in PHP
- How does PHP store data in cache?
- How to detect a mobile device using PHP?
- How to send HTML form data to email using PHP?
- How to get location from IP address using PHP?
- How to lock a file using PHP?
- How to import a CSV file into MySQL using PHP
- How to fetch data from database in PHP and display in PDF
- How to insert image in database using PHP
- How to remove last character from string using PHP?
- Write a PHP program to reverse a string without predefined function
- Write a PHP program to calculate percentage of total
- How to sanitize input for MySQL using PHP?
- Write a program to calculate electricity bill in PHP
- How to send email with SMTP in PHP?
- How to Send Text Messages With PHP?
- How to convert stdClass object to Array in PHP?
- How do I import Excel data into MySQL database using PHP?
- How can I post a form without refreshing the page?
- How to sort table columns with PHP and MySQL?
- How to get current directory, filename and code line number in PHP
Solution:
Ajax is a technique to provide fast and dynamic web services. It updates or retrieves data asynchronously by exchanging data over the server. It has the ability to perform tasks on a web page without requiring a page refresh.
Database
To store the form data in the back-end, we must have a database. In this article, we have used the MySQL database.
So, let's first create a database name Company in MySQL and a table name employee using the following MySQL statement. You can either use your existing database or copy and paste the following command into your database.
CREATE TABLE IF NOT EXISTS `employee` ( `emp_id` int(11) NOT NULL AUTO_INCREMENT, `emp_name` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `phone` int(11) NOT NULL, `address` varchar(50) NOT NULL, `username` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, PRIMARY KEY (`emp_id`) )
employee_form.php
Next, create a simple form 'employee_form.php' to get information from employee. This form contains all the fields that the employee table has. In this, we have included the jQuery and Bootstrap libraries.
The above file includes a CSS file name style.css and a JavaScript file name formscript.js. On clicking the submit button, the formsubmit() method will be called, which is defined in formscript.js file.
style.css
The style.css file contains the basic styles of the form.
formscript.js
The formscript.js file contains validation code, and the else part of this code contains ajax codes which send the request to the PHP script with the form data and return notification of a successful data submission. It sends the request without reloading the form.
storeemdata.php
In the above code, ajax calls the storeemdata.php file to store the form data. Generally, we would send data to a server using a POST request. The server handles it and sends a response back to the front-end. Instead of this, we have established the back-end connection here and captured the form data using JavaScript, and then sent an asynchronous request to the server to handle the response. We have used the improved version of MySQL. Make sure to replace the database credentials with yours.
The above code submits the form data to the employee table without reloading the page. After submitting the data, it returns 'Data inserted successfully' message and in case of failure, it returns an error message.
need an explanation for this answer? contact us directly to get an explanation for this answer