PHP - FORMS
Hello All...
welcome every one. I'm going to start PHP lectures for Very Beginners, who
wants to learn PHP from very beginning. No matter if you do not have any basic
programming concept. If you follow my tutorials I hope you will surely get some
how good knowledge of PHP.
Today our
Topic is
PHP-FORMS
Forms are used to send/receive data from one page to another or even from
one server to other server using user input. Today we will learn how to send
data from one page to another and saving record from PHP-forms to MySQL server.
There are two main methods / ways through form data can be sent and received.
i.
GET Method ($_GET)
ii.
POST Method ($_POST)
Both of these are also called as Global Variables. Global
variables means they can be accessed from multiple pages. Whenever we are
sending our form data using GET method means all the sent data will be shown in
URL of browser and it can be easily viewed by your neighbors. So it’s required
to not to use GET method whenever you are going to send your critical
information like passwords, credit card number, cnic number etc.
And when you have a critical information you are required to send your
data using POST method, as it is more secure and the data will not be shown in
the URL.
How we send our form data let’s look for an example. Hope that you are
familiar with forms and form elements.
<form action = “#” method = “get”>
<input type = “text” name = “first_name” />
<input type = “password” name = “password” />
<input type = “submit” name = “submit” />
</form>
action means where we are going to send our form data. # means at the
same page and the method we have used is get. It’s important to assign name to
each of these form elements as data will be received by their names. When you
fill these text fields and click the submit button means data we are sending to
the same page and it should be shown on the same page. Above we have sent our
data and how to receive that data is described below.
<?php
If(isset($_GET[‘submit’])){
echo “<br />”;
echo $_GET[‘name’];
echo $_GET[‘password’];
}
?>
If you run this code you will find result as shown in figure.
As you can see this is submitted form and what the data was filled is
shown at the same page but it is also shown on the URL as mentioned. Your name,
password and everything you will be going to send your critical information
will be visible to everyone. This is the main disadvantage of sending your data
using GET method.
Coming to PHP code first of all we are checking that either the form is
submitted or not if form is submitted then below code will run otherwise that
code will not work. And it’s mandatory to use if condition whenever you are
going to send/receive any form data. `isset` function is used to check either
the submit button is clicked or not, its argument is name of button what is
assigned in HTML code with what method data is sent with that same method. After
that if we want to echo value of form elements we will call with the name of
that element along with method.
Remember to write this html and php code on the same page if path of
action in the form attribute is same and save it with .php extension. You can
write PHP and HTML code on different pages you just give path of other file in
the form attribute.
Now we will be sending our data using POST method.
Whole process is same as that of GET method but where we used GET will be
replaced by POST.
<form action="#" method="post" >
<input type="text" name="first_name" />
<input type="password" name="password" />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST['submit'])){
echo $_POST['first_name'];
echo $_POST['password'];
}
?>
The result is shown in figure.
No data is shown in URL, and no one can look at your privacy whatever you
filled in form will not be visible to anyone. This is the main and important
advantage of using POST method.
Hope you got the concept about GET/POST methods further you try yourself
by submitting forms at different pages with multiple form fields.
Thanks Guys.
Any thing missing or any question from this topic you can comment below, I will
be trying to solve and answer your question.
Our next
topic will be `User Interface`
Good Luck J
No comments:
Post a Comment