All TalkersCode Topics

Follow TalkersCode On Social Media

How To Connect HTML Form To MySQL Database Using JavaScript

Last Updated : Jul 1, 2023

How To Connect HTML Form To MySQL Database Using JavaScript

In this tutorial we will show you the solution of how to connect HTML form to MySQL database using JavaScript, this method used for collects user information and stored into database.

When store any information to database we can do retrieve, update, delete or insert anything at anytime.

More number of records we can maintain in database.

Step By Step Guide On How To Connect HTML Form To MySQL Database Using JavaScript :-

First we have to create HTML form and what are the information we want to collect those elements are needs to declare within <form> tag. When form is submitted by user action attribute will load the php file.

Php is server side language here we used to store user data into database. For achieve database connection we need to collect server name, host name, password, database name like those informations.

All form element values are stored into separate variable by POST method.

Then insert query used for inserting user inputs into respective column of table in database.

When successfully data’s inserted alert message will display on webpage.

<!DOCTYPE html>
Connecting html form to database using php
<form method="post" action="dbcon.php">
    <input type="text" name="name" placeholder="Username">
    <input type="text" name="pswd" placeholder="Password">
    <input type="submit" name="submit" value="SUBMIT">
$conn = mysqli_connect($servername,$username,$password,$dbname);
    die("ERROR: Could not connect".mysql_error());
    $uname = $_POST['name'];
    $password = $_POST['pswd'];
    $sql = "INSERT INTO user VALUES ('$uname','$password')";
    if(mysqli_query($conn, $sql)){
        echo "Inserted record successfully";
        echo "ERROR: Sorry :".$sql."".mysqli_error($conn);
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and some websites when need to use external files those links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If your not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In <form> tag we used two attributes action and method, action attribute specifies where to send form data when form is submitted and method have two values GET,POST we are using POST method.
  7. <input> tag specifies an input field where the user can enter data. <input> tag can display in several ways depends on the attribute type.
  8. We are using in three ways for getting username, password as text and submit. Within <input> tag name attribute used for access in php file, placeholder attribute used for user can identify what type of input needs to enter.
  9. When user filled input field and click on submit form action attribute loads the dbcon.php file. Dbcon.php is a separate file contains database connection, query execution information.
  10. Before writing php file we need to create database with the name ‘user_records’ on mysql server then create a table with two columns namely username, password. Table name is ‘user’.
  11. In php file $ symbol used for declare a variable before variable name and syntax is $variable_name. $servername, $username, $password, $database are variables for storing respective values.
  12. Mysqli_connect() function used for create new connection and its stored into variable $conn. If(!$conn) condition checks connection is not correct means it will display error message on webpage by using die() method.
  13. In $_POST is a global variable in php used for collect form data. Isset() for checking whether a variable is set or not.
  14. if(isset($_POST['submit'])) condition is checks whether the submit value is set or not. If isset() condition is yes means user input values stored to respective variables.
  15. When storing user inputs into database we need to execute insert query. Insert query syntax is INSERT INTO table_name (‘value1’,’value2’,..).
  16. Then $uname, $password values insert to user table and query result will stored into variable $sql.
  17. mysqli_query() function performs a query against a database. if(mysqli_query($conn, $sql)) condition checks whether the insert query executed on database or not.
  18. If the query executed successfully, records are stored to database and display successful message on webpage. Otherwise it shows error on webpage.
  19. mysqli_close() function used for close the database connection finally.
  20. When executing html file before we need to run mysql server like xampp, wamp,.. then only we can get the result.
  21. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

I hope this tutorial on how to connect HTML form to MySQL database using JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

This topic helps you to understand the concept of html form, database and php.

Using html form, php and database we can collect and maintain more types of information like students personal information, students mark detail, office employee details, salary details and secure informations, etc..,

In conclusion now we able to connect html form to mysql database using php.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪