World Wide Web Programming 8 – Creating Some Interactivity
Creating a form A form in HTML is written around the tags … Form can use the following attributes
• ACTION: gives the address of the script to call • METHOD: gives the HTTP method selected (GET or POST)
Adding elements to the form
Use the tag Can take the following attributes • • • •
NAME: name of the input TYPE: see next slide for details VALUE: default value CHECKED: tells if a checkbox or button is selected by default • MAXLENGTH: Number of characters max
SIZE: can be use to define the length of the textbox
You can also use … • Attributes are NAME, ROWS and COLS
Finally … with NAME and SIZE • For each element you must use
The attribute TYPE
Tells what type of input we use • TEXT • PASSWORD • CHECKBOX • RADIO • HIDDEN • RESET • SUBMIT
Basic Structure
…
Difference between GET/POST
The GET method sends the data of a form right after the URL like this… • Script.php?name=Steven&nick=Nyraus
Therefore it is possible to see all the elements flowing from page to page The POST method sends directly the data to the script mentioned in “action”
How to access to the data
Once you get to the script, it is possible to access the variables It used to be possible to access those variables by putting just a $ followed by the name of the input you had in the form IMPORTANT! This is no longer valid in PHP 4.3.0 You need to use the environment variables $_GET and $_POST • Example: echo $_POST[“name”];
Dynamically creating elements
Simply run a PHP script that will contain the form and will add inputs dynamically For example, if you have an array with a list of files you could do: •
Transferring data between PHP scripts It’s based in the GET method If you want to call a script.php script, and make sure it receives a certain set of variables (var1, var2) set to 1 and 2 (for example) you write:
•
Link
Once you are in the script you can access var1 and var2 with • $_GET[“var1”] and $_GET[“var2”]
What is a cookie?
cookie, cooky (BISCUIT) noun [C] ESPECIALLY AMERICAN a sweet biscuit Would you like some cookies with your coffee? I've just made some chocolate-chip cookies. cookie (PERSON) noun [C] AMERICAN INFORMAL a person She's a smart/tough cookie. cookie (COMPUTING) noun [C] SPECIALIZED a piece of information stored on your computer which contains information about all the Internet documents that you have looked at
More seriously…
Created by Netscape Cookies can represent a text information (4000B max) You can have up to 20 cookies per domain Cookies can only be returned to the defined server in the specific folder A browser can hold up to 300 cookies Available cookies are imported by PHP as variables with the name of the cookie
Why would you use cookies? Memorizing a basket in a eCommerce site Individualized websites Identifying users
Cookies specifications
The header of a Set-Cookie holds the following attributes • • • • •
NAME=VALUE expires path domain secure
Ex: • Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
The DATE has to be set in the following format • Weekday DD-Mon-YYYY HH:MM:SS GMT • Ex: Monday 12-Jun-2000 11:00:00 GMT
How to create a cookie in PHP
setcookie(name, value, expires, path, domain, secure); Ex: