Submit HTML Form Data / Values to Database in PHP | Submit HTML/PHP Form data/values in Database
In this tutorial we will discuss about Submitting the HTML Form or PHP Form in the Database first we will submit it without the use of Jquery which will refresh the page after that we will implement the Jquery for submitting the value without refreshing the page in next tutorial.
Let’s Start 🙂
Steps to complete this tutorial.
Step 1. We will make an HTML Form.
Step 2. We will define HML Form Action to PHP Page
Step 3. Making Database.
Step 4. PHP page to Read Form values in Save the Values.
Step 5. Submitting the value to the PHP Page
Step 1. Making HTML Form
Here we will write the code to make the html form. We will use the table here to make the form. We use only 2 fields and 1 submit button.
<form> <table> <tr> <td>First name</td><td>:</td><td><input type="text" id="firstname" name="firstname"></td> </tr> <tr> <td>Last name</td><td>:</td><td><input type="text" id="lastname" name="lastname"></td> </tr> <tr> <td></td><td></td><td><input type="submit" id="savevalue" name="savevalue"></td> </tr> </table> </form>
This is your form will look like and now we will make the link of this form to our PHP page.
Step 2. Form action to PHP Page
So we have done the HTML form earlier now we will send our values to PHP page.
Suppose we have PHP page named getValues.php. Now we will define the action in our HTML Form.
We will replace the above <form> code with this line of code
<form action="getValues.php" method="post" name="Myform" id="Myform">
other code will remain same. Now why we change this and why we add these attributes. So let me tell you one by one.
1. Action=getvalue.php – When we will press the submit button this action will take our values to PHP page that is getValue.php.
2. Method=post – Right now i just tell you a little bit about this. When we us post than the URL will not show the parameters but the values are still there in the memory. We can use the “get” on the place of “post”. Get will show your values in the url parameter while checking the login information we always use the “Post”.
So here we finishes with the Action attribute of Form
Step 3. Making Database.
Now after the HTML form and action attribute we have our values now what to do with these values. Lets play some tic tac toe or football with the values…. Lol …… We will make a Container to hold these values and save them. For saving the values we have many options like Array/Session (But will not retain your value after your browser closes), Cookies (Not a good idea there is a limit and if you change your pc it will show error), XML, Database. So here we will use the Database option and we use the mySql for this.
First of all we will create a database and name it myinformation.
create database myinformation
Now will make the table
we will use 2 column here in table these are Firstname,Lastname.
create table info ( firstname varchar(50), secondname varchar(50) )
Now we have completed our database structure and we will now move to our next step that is making our PHP page.
Step 4. PHP page to save the values.
Now we will make our PHP page to get the HTML Form values and save them in our Database. First we will get the values.
<?php $getfirstname=$_POST["firstname"] $getlastname=$_POST["lastname"] ?>
From above code we will get the Form Values in these variables. $Firstname will put the firstname value and $lastname will put the lastname value.
If we use the GET at our HTML form than we will change the POST to GET.
Now we will Establish the connection with our database client to connect our database.
<?php $hostname="localhost" $username="root" $password="sa" $databasename="myinformation" // these all are variable and all of them having some values. we have made the myinformation earlier //Connecting to the database $con=mysqli_connect($hostname,$username,$password,$databasename); // Checking if it is connected or not if($con) { // If it is connected than we will save our values else show the error //Now we will insert values into the database //We will run the insert query. $savevalues="Insert into info values ('$firstname','$lastname'); $result=mysqli_query($con,$savevalue) or die("Error in query"); if($result) { echo "Value Saved". } else { echo "Error in saving the value please try again." } } else { echo "Error in connecting the database." } ?>
Now we have completed writing of PHP page which include the code to get the values from HTML Form and save them to the Database.
Step 5. Submitting the value to the PHP Page
Waiting for what ???? we have completed everything just try your code and see weather it is working or not. If there is any bug than let us know we will try to remove it.
Fill your form and press submit and see will it save or not.
Thank You for reading this article. Happy Coding.
1 Comment