Most people think of a Web page as
nothing more than a collection o f HTML code. This is fine if you happen to be a
Web designer—but, as a PHP developer, your knowledge must run much deeper if
you want to take full advantage of what the Web has to offer.
From the point of view of the Web
server, the generation of a document starts with an HTTP request, in which the
client requests access to a resource using one of a short list of methods. The
client can also send a data payload (called request) along with its request—for
example, if you are posting an HTTP form, the payload could consist of the form
data, while if you are uploading a file, the payload would consist of the file
itself.
Once a request is received, the
server decodes the data that it has received and passes it on to the PHP
interpreter (clearly, we are assuming that the request was made for a PHP
script—therwise, the server can choose a different handler or, in the case of
static resources, such as images, output them directly).
Upon output, the server first writes
a set of response headers to the clients; these can contain information useful
to the client—such as the type of content being returned, or its encoding, as
well as data needed to maintain the client and the server in a stateful
exchange (we’ll explain this later).
Forms
and URLs
From an HTML perspective, the
difference between GET and POST is limited to the action attribute of the
<form> element:
<!--Form submitted
with GET-->
<form
action="index.php" method="GET">
List: <input
type="text" name="list" /><br />
Order by:
<select
name="orderby">
<option
value="name">Name</option>
<option
value="city">City</option>
<option value="zip">ZIP
Code</option>
</select><br
/>
Sort order:
<select
name="direction">
<option
value="asc">Ascending</option>
<option
value="desc">Descending</option>
</select>
</form>
<!--Form submitted
with POST-->
<form
action="index.php" method="POST">
<input type="hidden"
name="login" value="1" />
<input type="text" name="user"
/>
<input type="password"
name="pass’ />
</form>
GET
and URLs
When a form is submitted using the
GET method, its values are encoded directly in the query string portion of the
URL. For example, if you submit the form above by entering user in the List box
and choosing to sort by Name in Ascending order, the browser will call up our
index.php script with the following URL:
As you can see, the data has been
encoded and appended to the and of the URL for our script. In order to access
the data, we must now use the $_GET superglobal array.
Each argument is accessible through
an array key of the same name:
echo $_GET[’list’];
You can create arrays by using array
notation...
..and then access them using the
following syntax:
echo $_GET[’order’][’by’];
echo $_GET[’order’][’direction’];
Note that, clearly, there is nothing
that stops you from creating URLs that already contain query data—there is no
special trick to it, other than the data must be encoded using a particular
mechanism that, in PHP, is provided by the urlencode() function:
$data = "Max & Ruby";
echo "http://www.phparch.com/index.php?name="
. urlencode ($data);
The PHP interpreter will
automatically decode all incoming data for us, so there is no need to execute
urldecode() on anything extracted from $_GET.
Using
POST
When sending the form we introduced
above with the method attribute set to post, the data is accessible using the $_POST
superglobal array. Just like $_GET, $_POST contains one array element named
after each input name.
if ($_POST[’login’]) {
if ($_POST[’user’] == "admin"
&&
$_POST[’pass’] == "secretpassword")
{
// Handle login
}
}
In this example, we first check that
the submit button was clicked, then we validate that the user input is correct.
Also, similarly to GET input, we can
again u se array notation:
<form
method="post">
<p>
Please choose all languages you currently know
or would like
to learn in the next 12 months.
</p>
<p>
<label>
<input
type="checkbox" name="languages[]" value="PHP"
/>
PHP
</label>
<label>
Web Programming ” 101
<input type="checkbox"
name="languages[]" value="Perl" />
Perl
</label>
<label>
<input type="checkbox"
name="languages[]" value="Ruby" />
Ruby
</label>
<br />
<input
type="submit" value="Send" name="poll" />
</p>
</form>
The form above has three checkboxes,
all named languages[]; these will all be added individually to an array called
languages in the $_POST superglobal array—just like when you use an empty key
(e.g. $array[] = “foo”) to append a new element to an existing array in PHP.
Once inside your script, you will be able to access these values as follows:
foreach ($_POST[’languages’]
as $language) {
switch ($language) {
case ’PHP’ :
echo "PHP?
Awesome! <br />";
break;
case ’Perl’ :
echo "Perl? Ew.
Just Ew. <br />";
break;
case ’Ruby’ :
echo "Ruby? Can
you say... ’bandwagon?’ <br />";
break;
default:
echo "Unknown
language!";
}
}
0 Comment to " PHP Web programming - form post - get urls "
Post a Comment