learn

Create form with text box and submit button

category: Ask
Created by: Dr.Ferrous

Create form with text box and submit button

category: Ask
Created by: Dr.Ferrous
twitter google+ facebook pinned 

Question:

Hi, i am a beginner and i want to create a php form where i need a text box and a submit button. Whenever someone type anything in a box then it should be posted.. I want your help how to make it.. Thanks

by Zohaib ali

Answer:

Step 1: Creating the HTML form

Create a file named myform.php or whatever you like, and put the following HTML:

 

Submitting the form in this lesson will not work, because there's no file named "myform.php" in this site.

Step 2: Getting the form data

When you click on submit button, all inputs in the form will be sent to file "myform.php", which is the same file that contains the form. So, we will add at the top of the file "myform.php" some php script to get those sent data.

myform.php should be like this:

Remember, this form submits to itself, meaning that the data of form will be sent to this page, and the page will load again from the top.

We write "if($_POST['submitButton'] == 'Submit'){" to know if the page is being submitted by the form or not.

If the page is opening normally, "if($_POST['submitButton'] == 'Submit'){" will return false.

And if the page is being submitted by the form, then "if($_POST['submitButton'] == 'Submit'){" will return true.

Step 3: saving the data

Now after we get the data of the form which is $varName, we will save it in the database.

To learn about saving in database, please go to this lesson: MySQLi