CSE625 - Basic php
Sending data via the URL / Sending data via a form
This REALLY makes websites work well
php is really cool when you're ready for a good head spin. Or when you're ready to consider a major website with many pages and the possibly of updates in the future. Or if you wish to create files and databases. We'll use some php but it will best for intellignet-looking conversations around the water cooler. Do any of us actually have water coolers in your office arena?
One simple php process
While php can be very interactive, we will consider first the "include" statement. It will require an single line of code in the main webpage and a secondary document which has additional code which the server will add to your main webpage without the user even knowing it.
The code is
<?php include '(officehours.php)'; ?>
where the name of the secondary document
is called officehours.php and looks something like this:
print '
Fall office hours: Mondays and Wednesdays - 9:00AM to noon
Or contact me by phone (503-555-1234)
Or by email at <a href="mailto:saxowsd@wou.edu">saxowsd@wou.edu</a>
';
?>
There are some limitations what can be coded into the secondary document, but most basic coding will work.
This is usefulwhen you have a large of webpages with a common section that may change from time to time. The example of the office hours is used because the office hours are a part of all the webpages for all the classes and will probably change every term but the contact of most of the class webapges may not.
A simple php transfer of data using the URL
There are several ways to transfer information from one webpage to another. One is to enter the information using <form>, another is entering the information in the URL. The more elaborate is using databases.A sample code for using the URL is
Good morning, <?php echo $_GET["name"]; ?>!
where
the name will come from the URL. The URL may look like this (the crtical part is the $name=Whatevername
http://www.wou.edu/phpdocname.php?name=Aname
which will generate a webapage looking like this:
Good morning, Aname!
A simple php transfer of data using a form
In a php webpage, use code similar to the following:
<form action="second.php" method="get">
<input type="text" name="name" />
<input type="submit" />
</form>
Good morning, <?php echo $_GET["name"]; ?>!
When you open the first webpage and complete the form with a name such as Joesph, and click on submit, the second webpage will open and look like the following:
Good morning, Joseph!
You can repeat the above coding with one change and have similar results. In the first webpage change "get" to "post" and in the second webpage change "GET" to "POST" (must be capitals). The resulting webpage will be exactly the same but the URL will be different. With the "get" you will see something like ?name=Joseph in the URL, whereas you will not see that code in the URL of the second webpage.