form action

advertisement
PHP and XHTML Forms
Getting data from the user
Forms
●
●
●
If we can't get any data from the user, there's not
a lot of point in tailoring a page for that user
One of the main ways we get data from the user
is through forms
– The other main way is direct from the browser,
which sends strings identifying itself, language,
operating system etc
We studied XHTML forms when we looked at
JavaScript in CS15020
October 2012
Web Programming
2
XHTML Forms
●
If you recall, we had:
– <form action="">
–
http://users.aber.ac.uk/ais/examples/JavaScript/inputexample.html
Why? Because we weren't sending the result of
the form anywhere; we were handling it on the
client side with the JavaScript we wrote
● But now, we want to send the content of the form
elements back to a PHP script on the server
● The action is the name of the script
● And we need a <input type="submit" ...> which
October
2012 us a button Web
Programming
3
gives
to submit
the form too
●
GET and POST
●
●
●
There are two methods for sending the
data to a script
GET <form action="my.php" method="get">
POST <form
October 2012
action="my.php" method="post">
Web Programming
4
GET
●
●
●
●
Passes the data as part of the URL – you
will have noticed this before, in your
browser, I am sure
The HTTP request is just headers
There's a character limit – beware! And it's
browser dependent
ASCII only – no special characters allowed
–
October 2012
Problem if, say, user has a name or
address with
an accented character
Web Programming
5
POST
●
Sends the data as name-value pairs, in the
body of the HTTP request
–
●
which is only seen by the server
Because the data is in the body,
–
–
October 2012
there is effectively no size limit,
it can contain any characters
Web Programming
6
$_GET
●
In PHP, there is a variable $_GET
Used to access the contents of a form
submitted with the GET method
●
http://users.aber.ac.uk/ais/examples/php/inputexample.html
●
●
●
Almost the same as we did for JavaScript, but
without the JavaScript
The form is identical apart from the form's
action and method, and the final button (it is a
type=submit rather than a type=button and it
doesn't call a JavaScript function)
October 2012
Web Programming
7
$_GET – the PHP script
<p>Here is what you ordered:</p>
<ul>
<li><?php echo $_GET["drink"];?></li>
<?php
if (array_key_exists("sugar", $_GET))
echo "<li>" . $_GET["sugar"] . "</li>";
if (array_key_exists("cinnamon", $_GET))
echo "<li>" . $_GET["cinnamon"] . "</li>";
?>
<li><?php echo $_GET["creamormilk"];?></li>
</ul>
October 2012
Web Programming
8
When to use GET
●
Notice the URL bar when we use GET
✔
✗
It contains our variables – so we can bookmark it
But therefore it shouldn't be used for sensitive
information (passwords, bank details, etc)
And it has a character limit (browser
dependent, but to be safe, recommendation
is to keep within 256 – and ASCII only)
● If information is not sensitive, and character
limit is not a problem, then use GET
✗
October 2012
Web Programming
9
$_POST
●
●
●
●
PHP has a variable $_POST, which
does much the same as $_GET, but for
data submitted using the POST method
We can re-write our last example, using
the post method instead of the get
method
All that changes is GET => POST in a
few places
http://users.aber.ac.uk/ais/examples/php/postexample.html
October 2012
Web Programming
10
$_POST – the PHP script
<p>Here is what you ordered:</p>
<ul>
<li><?php echo $_POST["drink"];?></li>
<?php
if (array_key_exists("sugar", $_POST))
echo "<li>" . $_POST["sugar"] . "</li>";
if (array_key_exists("cinnamon", $_POST))
echo "<li>" . $_POST["cinnamon"] . "</li>";
?>
<li><?php echo $_POST["creamormilk"];?></li>
</ul>
October 2012
Web Programming
11
When to use POST
●
Did you notice the URL?
✔
✗
It didn't contain any variables – so it's more
secure
But that means you can't bookmark it and
expect the same results another time
There is no (practical) limit to how much
data can be passed
● Use POST for sensitive data, or if the
amount of data is too large for GET
✔
October 2012
Web Programming
12
Just associative arrays
●
$_GET and $_POST are in effect no more
than associative arrays
–
●
(arrays indexed by key, not by number)
So you can use all the usual array
functions on them
–
as I did in my example – using
array_key_exists("sugar", $_POST)
That makes it very easy to handle input
from forms in your script
● Use reference material when you need a
October
2012
Web Programming
function
like this (e.g.
use w3schools!)
●
13
Suggestion
●
●
●
Grab my code (PHP in these slides,
XHTML from the web link) and try it for
yourself
Write your own examples, based on my
code
This stuff is crucial; please do make sure
you understand it
–
October 2012
Don't leave it – do it now!
Web Programming
14
Download