IPT home IPT A Virtual Approach by Peter Whitehouse
Quick Links:
 
 
Information and Intelligent Systems Social and Ethical Implications Human Computer Interaction Software and Systems Engineering eXercise Files Course Outline and Assessment A-Z of Geeky Acronyms Terrace Work Program 2004 Sillybus FAQ = Frequently Asked Questions Help
 
 

PHP Interaction

Input-Output

Home | Variables & Operators | Input & Output | Decisions & Loops| Sub-Programming | Built-ins

Output as Web

In general, the "output" from PHP commands is HTML, or page-rendered content. We have to exlicitely output values to see them on the web page displayed at the client end. All this markup is done at the server and sent (via http) to the client. It is not as complicated as it sounds ... but ... some PHP enabled websites ALSO employ Java, CSS, Javascript and other languages as parf of the page contents to further effect what the resultant page looks like in the browser. We will focus here on only PHP output, but there is LOTS you can do to jazz up the output of your pages.

 

Input Through Web

There are a few ways to use a web page to collect user intent and feed that inten into your PHP scripts. Because the scripts are actually run on the SERVER however, we will only concern ourself with means that result in stuff being SENT to the server (most commonly through HTML forms).

HTML Forms are easy to set up and contain a number of powerful things to control the data capture process. Forms may have text input spots, buttons, password fields, like many of the things we would recognise as forms, and a bunch of things that do not look very form like at all.

 

Value Passing

One view of a web page and the next view of the page could/should be thought of as separate "runs" of the code that page is comprised of. It is complicated if there is PHP code on the server source, particularly if that code relies on values stored in variables or input gathered from the last time the page was displayed.

There are 4 common methods used to effect value transfer between pages (or indeed refhreshes of teh same page)- POST and GET methods of forms, Cookies and Sessions.

POST, GET and SESSION variables are all stored on the SERVER, but GET variables are also VISIBLE in the URL of the referring page. COOKIES are local TEXT files, stored in the cache of the client browser and therefore could be considered insecure if storing important things like passwords, pin numbers, bank account details etc.

Most sessions set a user-key on the user's computer that looks something like this: 765487cf34ert8dede5a562e4f3a7e12. Then, when a session is opened on another page, it scans the computer for a user-key. If there is a match, it accesses that session, if not, it starts a new session.

We choose the location our data is stored to suit the application and required security. Hackers know this also. Securing data, important information and authentication details is an artform. One could argue that online security is an oxymoron.

One method of allowing the user to interact with web pages is via a FORM.

One way of dealing with the user interaction from a form is using PHP.

Within a page, a form can be defined to allow the user to fill out boxes and submit their information.
Suppose we have 2 web pages. In firstpage.html, we have the following HTML code:


<form name="myfirst" method="post" action="secondpage.php">
<p>Name:<input type="text" name="who" value="type your name here"></p>
<p>BirthYear: <input type="number" name="byear" value="1990"></p>
<p><input type="submit" name="submit" value="Post My Details"</p>
</form>

notes: the "form" is a container for interactive things (like type in text boxes and buttons). Note the ACTION names the page the form will automatically bounce to when the submit button is pressed.

In secondpage.php, we have the following PHP and HTML code:

<?php
//get posted data
$username = $_POST['who'];
$birthyear = $_POST['byear'];

//say gidday
echo '<p>Hi there ' . $username . ', it is nice to meet you.</p>';

//work some stuff out
$age = 2017 - $birthyear;
printf('<p>Being born in %s, that makes you %s years old</p>',$birthyear,$age)
?>

<p><a href="firstpage.html">Return to Data entry page</a>

Notes: We use $_POST to retrieve the things stored there by the first page. I have used 2 methods of output here - echo is fine, printf is more sophisticated but both are commonly used.

We can do the whole task above in the SAME webpage by adding a little test to see if the "SUBMIT" button has been pressed. I would make a page called "meetngreet.php" and fill it with something like this:

<form name="mysecond" method="post" action="meetngreet.php">
<p>Name:<input type="text" name="who" value="type your name here"></p>
<p>BirthYear: <input type="number" name="byear" value="1990"></p>
<p><input type="submit" name="submit" value="Post My Details"</p>
</form>
<?php
if isset($_POST['submit']){
//get posted data because they pressed the submit button
$username = $_POST['who'];
$birthyear = $_POST['byear'];

//say gidday
echo '<p>Hi there ' . $username . ', it is nice to meet you.</p>';

//work some stuff out
$age = 2017 - $birthyear;
printf('<p>Being born in %s, that makes you %s years old</p>',$birthyear,$age)
} else {
echo '<p>Please fill out the boxes above and press submit</p>';
}
?>

 

 

wonko@wonko.info
©Copyright t 1992..2017+. Edition 25.150117
wonkosite
Creative Commons License
This work is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 2.1 Australia License
.