Hi ,
I just want to clear some of the basic confusion on the above topic that I know at the beginning stage every one get threaten of codes and logics. I just want to clear your confusion guys . Just relax and try to follow my recent posts I will make you sure that you will learn this technologies have question ask I will try to give you the best answer as I can . Lets Go...
Okay ,
what happens when any one press the form's submit button ? who is responsible of handling all the data that the user fills in that form ? For all these questions you have to solve the questions below :-
Q.1. What is the Role of form
Q.2. What is the Role of PHP
A.1-> Forms, that we create in HTML is just serve as an front-end (The thing that is visible to the client or user) . Forms has various fields such as text-fields, select boxes , button , file button , checkboxes , radios , password fields and all these fields take some user inputs that the user fills in the form's fields.
A.2-> PHP , think of this as a person who is hidden but doing all the logical stuff for you . For example , suppose two persons are sitting in front of each other one is asking questions to another please give the sum of 2+2 ?
Now , the person who asks question is client or user and the ears are your career form and the hidden(your brain) is your logic server PHP , who process the data that is received by your ears and sent to you and prepare the answer and send it back to you at different module in this example mouth which delivers the result back to the person who asks the question . In the same way all the things works.
Here , we go programatically :-
code for index.html
----------------------------------
<form method="post" action="process.php">
Visitor please enter your name?<input type="text" name="v_name" />
<input type="submit" name="v_check" value="Press Me" />
</form>
===============================================
code for process.php
---------------------------------
Now , in the above code form is the career of all the data which is comming in all its form's fields in our example there is only one field which takes the visitor's name and stores that value in a variable called "v_name" . Actually ,
the name attribute of any form field is used to declare that form field as a variable and remember that variable is only responsible for storing the value that is entered in that field.
Now , when any user/client open the page index.html in browser he/she will get
a question "Visitor please enter your name?" and a text box to enter the name and as soon as the client enters his /her name in that text field and press the submit button whose name is "v_check" the form will try to send the data at that location where form's action is defined in our case it is "process.php" and take all the data that is fed in text box with an algorithm that is defined in method attribute of a form in our example it is "post" .
There are basically two algorithms that is used in form submission and that are :-
1. GET : - If this algorithm is used the data will be visible in url of your address bar when the submit button is pressed . Never use when data is secure like userid or password , bank account details , you cannot send or receive large files like Binary data or long textual data .
2. POST :- It outwitts all the limitations of GET that is expressed above . and the data that is send while form submission will not shown in url .
Now , remember at the page where form's action is defined in our case it is "process.php" we use a global variable (the predefined variable in php) according to form's method in our case it is "post" . So, we use $_POST in case if form uses method as method="get" we use $_GET to receive the data that is travelled in url to reach at the location "process.php" .
<?php
if(isset($_POST["v_check"])){
$visitor_name=$_POST["v_name"];
echo "Welcome :$visitor_name";
}
?>
In the above code I have used isset($_POST["v_check"]) to confirm and check that the submit button present at "index.html" is pressed or not if pressed or set then only execute that block of code and $_POST["v_name"] is used to cath the value that is stored in a text box whose variable name is "v_name" and then I just put that value in another variable called "$visitor_name" and then finally echo the or print the message . Output of the above code is :-
Output : Welcome :Xyz(the name that is entered in the text box will appear here I have just written Xyz for an example don't get confused.).
Hope , The thing that I have explained to you will clear many confusion and strengthen your base .
All the best , try reading my posts and asks any new questions or doubts if you have . I will be glad to help you all at any time .
Thanks