How to Submit a form using HTML and PHP edit button Edit

souviksarkar1396@gmail.com | calendar 12 December 2020 | 3403

Hello everyone, today we will se how to create a simple HTML form and Send data to backend using PHP. HTML is a language that is used to show content on any webpage and PHP is a server side scripting language.

HTML Code -

First of all open any code editor and create a file named simpleform.html. Then write the following code.

<form action="">
        <label for="firstName">First Name</label>
        <input type="text">
        <label for="lastName">Last Name</label>
        <input type="text">
        <label for="email">Email</label>
        <input type="email">
        <button type="submit">Submit</button>
To create a form in html we need to write it in form tag. HTML is liable for only to show items on the webpage. Now lets write some PHP code to send data into backend. Before that we need to add some more things into the existing HTML code. Lets add that first and understand why we need to insert those tags.
<form action="action.php" method="POST">
        <label for="firstName">First Name</label>
        <input type="text" name="firstName">
        <label for="lastName">Last Name</label>
        <input type="text" name="lastName">
        <label for="email">Email</label>
        <input type="email" name="email">
        <button type="submit" name="submit">Submit</button>
Here we have included 3 new things into the existing HTML form. i) action - action is used to redirect the form data, that means after clicking the submit button the page action.php will run. next ii) method - There are tow methods 1) GET and 2) POST. Generally we use POST because it can not be seen by others. If we use GET, then the data can be seen from the outside. and last one is using iii)name field. It is used to uniquely identify each and every field of the html form.

PHP Code -

Now, lets start writing some PHP code. After successfully creating the form and all other important things now we are creating action.php file. Which will run after hitting the submit button. Lets write some code to access the data passed by the form. Go to action.php and write the following code -

$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];

echo $firstName;
echo $lastName;
echo $email;

$_POST is used to accessing the datas from the form if we used GET method then we need to write $_GET to access the values. If you can see the same data whatever you have inserted into the form field then Bingo!! you have done it. Now you just need to connect to your database and store the form data.