Archive:PHP and MySQL Programming/Form Handling: Difference between revisions
wikademia>The Kid New page: == Introduction == In order to get user input without using the command line, then you will use HTML forms. PHP has various functions and techniques for handling the input from HTML for... |
wikademia>Wikademia m moved PHP and MySQL Programming/Form Handling to Archive:PHP and MySQL Programming/Form Handling over redirect |
||
| (14 intermediate revisions by 4 users not shown) | |||
| Line 1: | Line 1: | ||
== Introduction == | == Introduction == | ||
In order to get user input without using the command line, then you will use HTML forms. PHP has various functions and techniques for handling the input from HTML forms. In specific, we will use the special array variables $_GET[] and $_POST[]. | In order to get user input without using the command line, then you will use HTML forms. PHP has various functions and techniques for handling the input from HTML forms. In specific, we will use the special array variables $_GET[] and $_POST[]. | ||
{{ntnes}} | |||
== HTML Forms == | == HTML Forms == | ||
Before we start using these special variables, lets have a look at how to set up the HTML forms to interact with PHP. Here is a simple example form : | Before we start using these special variables, lets have a look at how to set up the HTML forms to interact with PHP. Here is a simple example form : | ||
< | <source lang="html4strict"> | ||
< | <html> | ||
< | <head><title>HTML Form Example</title></head> | ||
< | <body> | ||
username : < | <form method="POST" action="form_handler.php"> | ||
password : < | username : <input type="text" name="username"> <br> | ||
< | password : <input type="password" name="password"> <br> | ||
</ | <input type="submit" value="GO"> | ||
</ | </form> | ||
</ | </body> | ||
</html> | |||
</source> | |||
The above example sets up a form with a | The above example sets up a form with a <code>method</code> and an <code>action</code> attribute. The `method` defines how the data from the form is to be transferred to the page defined by `action`. The `method` can either be ''GET'' or ''POST''. If ''GET'' is used, then all the form elements will be appended to the url of the target page (specified by the <code>action</code> attribute), in url encoded format (<code>target?attrib1=val1&attrib2=val2&attrib3=val3</code> etc...) These attributes are easily accessed in PHP by using the <code>$_GET[]</code> array. | ||
If POST is | If the ''POST'' method is specified, then the data will be put into a special server variable, and accessed in PHP by using the built-in <code>$_POST[]</code> array. | ||
Note that in the HTML code, the | Note that in the HTML code, the <code>name</code> attribute of the INPUT tags are used as the reference strings in the <code>$_GET</code> and <code>$_POST</code> arrays, thus it is very important to always use unique values for the <code>name</code> attribute. | ||
== GET == | == GET == | ||
To retrieve GET variables in the address url, we use the following method : | To retrieve <code>GET</code> variables in the address url, we use the following method : | ||
'''form_handler.php :''' | |||
<source lang="php"> | |||
<? | <? | ||
$username = $_GET['username']; | $username = $_GET['username']; | ||
$password = $_GET['password']; | $password = $_GET['password']; | ||
echo "Username : $username \n Password : $password \n"; | |||
?> | ?> | ||
</source> | |||
Please note that GET is not good to use for this example, because you will generally not want username and password details being displayed in the URL. Rather use POST for login details and authentication. | Please note that <code>GET</code> is not good to use for this example, because you will generally not want username and password details being displayed in the URL. Rather use POST for login details and authentication. | ||
== POST == | == POST == | ||
To retrieve POST variables, we use the following method : | To retrieve <code>POST</code> variables, we use the following method : | ||
'''form_handler.php :''' | |||
<source lang="php"> | |||
<? | <? | ||
$username = $_POST['username']; | $username = $_POST['username']; | ||
| Line 49: | Line 52: | ||
print "Username : $username \n Password : $password \n"; | print "Username : $username \n Password : $password \n"; | ||
?> | ?> | ||
</source> | |||
Note that when <code>POST</code> is used, the posted data is completely invisible to the user. | |||
[[../Command Line Programming/|Previous: Command Line Programming]] | [[../|TOC]] | [[../Introduction to MySQL/|Next: Introduction to MySQL]] | |||
[[Category:PHP | [[Category:PHP and MySQL Programming|{{SUBPAGENAME}}]] | ||
Latest revision as of 07:37, 12 June 2009
Introduction
In order to get user input without using the command line, then you will use HTML forms. PHP has various functions and techniques for handling the input from HTML forms. In specific, we will use the special array variables $_GET[] and $_POST[].
HTML Forms
Before we start using these special variables, lets have a look at how to set up the HTML forms to interact with PHP. Here is a simple example form :
<source lang="html4strict">
<html>
<head><title>HTML Form Example</title></head>
<body>
<form method="POST" action="form_handler.php">
username : <input type="text" name="username">
password : <input type="password" name="password">
<input type="submit" value="GO">
</form>
</body>
</html>
</source>
The above example sets up a form with a method and an action attribute. The `method` defines how the data from the form is to be transferred to the page defined by `action`. The `method` can either be GET or POST. If GET is used, then all the form elements will be appended to the url of the target page (specified by the action attribute), in url encoded format (target?attrib1=val1&attrib2=val2&attrib3=val3 etc...) These attributes are easily accessed in PHP by using the $_GET[] array.
If the POST method is specified, then the data will be put into a special server variable, and accessed in PHP by using the built-in $_POST[] array.
Note that in the HTML code, the name attribute of the INPUT tags are used as the reference strings in the $_GET and $_POST arrays, thus it is very important to always use unique values for the name attribute.
GET
To retrieve GET variables in the address url, we use the following method :
form_handler.php : <source lang="php">
<? $username = $_GET['username']; $password = $_GET['password']; echo "Username : $username \n Password : $password \n"; ?>
</source>
Please note that GET is not good to use for this example, because you will generally not want username and password details being displayed in the URL. Rather use POST for login details and authentication.
POST
To retrieve POST variables, we use the following method :
form_handler.php : <source lang="php">
<? $username = $_POST['username']; $password = $_POST['password']; print "Username : $username \n Password : $password \n"; ?>
</source>
Note that when POST is used, the posted data is completely invisible to the user.
[[../Command Line Programming/|Previous: Command Line Programming]] | [[../|TOC]] | [[../Introduction to MySQL/|Next: Introduction to MySQL]]