28 October

php class 2:-PHP Forms And User Input

The PHP $_GET and $_POST variables are used to retr
ieve information
from forms, like user input.
PHP Form Handling

The most important thing to notice when dealing wit
h HTML forms
and PHP is that any form element in an HTML page wi
ll automatically
be available to your PHP scripts.
be available to your PHP scripts.

Form example:
<html>

<body><form action="welcome.php" method="post">

Name: <input type="text" name="name" />

Age: <input type="text" name="age" />

<input type="submit" />
</form></body>
</html>

The example HTML page above contains two input fiel
ds and a submit button. When the
user fills in this form and click on the submit but
ton, the form data is sent to the
"welcome.php" file.

•The "welcome.php" file looks like this:

<html>

<body>Welcome <?php echo $_POST["name"]; ?>.<br/>

You are <?php echo $_POST["age"]; ?> years old.</bo
dy>

</html>

A sample output of the above script may be:

Welcome John.

You are 28 years old.
Form Validation

User input should be validated whenever possible. C
lient side validation is faster, and will
reduce server load.
reduce server load.

However, any site that gets enough traffic to worry
about server resources, may also need
to worry about site security. You should always use
server side validation if the form
accesses a database.

A good way to validate a form on the server is to p
ost the form to itself, instead of
jumping to a different page. The user will then get
the error messages on the same page as
the form. This makes it easier to discover the erro
PHP $_Get
The $_GET variable is used to collect values from a
form with method="get".
The $_GET Variable
• The $_GET variable is an array of variable names
and values sent by the HTTP GET
method.
• The $_GET variable is used to collect values from
a form with method="get".
Information sent from a form with the GET method is
visible to everyone (it will be
displayed in the browser's address bar) and it has
limits on the amount of information to
send (max. 100 characters).
send (max. 100 characters).
Example
<form action="welcome.php" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />

</form>
When the user clicks the "Submit" button, the URL s
ent could look something like this:
http://www.tops-int.com/welcome.php?name=Peter&age=
37

The "welcome.php" file can now use the $_GET variab
le to catch the form data (notice
that the names of the form fields will automaticall
y be the ID keys in the $_GET array):
TOPS Technologies - Version May, 2012
134
 
PHP $_Get
Welcome <?php echo $_GET["name"]; ?>.<br/>
You are <?php echo $_GET["age"]; ?> years old!

Why use $_GET?
Note: When using the $_GET variable all variable names
and values are displayed in the URL. So this method
should not be used when sending passwords or other
sensitive information! However, because the variables
should not be used when sending passwords or other
sensitive information! However, because the variables
are displayed in the URL, it is possible to bookmark
the
page. This can be useful in some cases.

Note: The HTTP GET method is not suitable on
large variable values; the value cannot exceed 100
characters
r.

Comments


Maria Joli | 12 July 2014, 10:20

Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.

Reply

John Doe | 12 July 2014, 10:30

Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.

Reply

Jane Doe | 12 July 2014, 10:40

Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.

Reply

Danny Dev | 12 July 2014, 11:10

Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.

Reply

Post Comments