Word version of the Advanced Web Topics supplement

advertisement
REFERENCES
www.w3.org is the official repository of standards involving the web. Of material in this
talk, the following items are based on standards there:
▪
▪
HTTP: this is the protocol for making GET and POST requests
HTML: this is the standard describing the way web pages are formatted. The
various tags used to define forms are described in this standard. Anyone who is
doing serious work with the web needs to have a copy of the HTML specification.
The form application is documented in a man page which you should be able to get by
typing “man form” on any system that has it.
The counter application described is documented at counter.rutgers.edu/counter.html.
More details are available from a reference at the bottom of that page.
For CGI programming in general, the O’Reilly book is an excellent reference. You’ll also
need a reference to whatever language you use. O’Reilly has excellent books on Perl
and Javascript.
Netscape and Microsoft both maintain extensive information in their web pages. Since
Javascript is a Netscape invention, their web area is a particularly good source for
information about it. It is developer.netscape.com. See particularly their “library”, which
has their documentation, examples, etc.
A good site for PHP is php.iquest.net for version 2 and www.php.net for version 3. The
official location for Minisql is ftp://ftp.bond.edu.au/pub/Minerva/msql.
BRIEF INSTRUCTIONS FOR FORM
To handle a form: run make_html_data to create ~/html_data. In html_data you need a
file XXX.form-spec, where XXX is the same name you supply in the form. The formspec file specifies the format of the lines that go into the corresponding XXX.form-data.
XXX.form-mail, XXX.form-ok, and XXX.form-error are optional templates to use for mail,
and the two possible responses pages.
foo.form-spec
filelimit=20000
replace=*/"/""/
test=*//
fileline="<$var1>","<$var2>"
mailline="<$var1>","<$var2>"
mailto=hedrick@geneva
foo.form-mail
From: hedrick@geneva
To: hedrick@geneva
Subject: form output
var1: <$var1>
var2: <$var2>
foo.form-ok
<html>
var1: <$var1><br>
var2: <$var2><br>
</html>
foo.form-error
<html>
Here is the error: <$ERROR>
</html>
JAVASCRIPT EXAMPLE
<html>
<script language="Javascript">
<!-- The text starting here is for Javascript only
function checknumeric(thing)
{
if (isNaN(parseFloat(thing.value))) {
alert('Value must be numeric');
thing.value = '';
}
}
function comptotal()
{
var f = document.mainform;
f.total.value = parseFloat(f.first.value) + parseFloat(f.second.value);
}
function checkform(f)
{
if (isNaN(parseFloat(f.first.value)))
{alert('first must be numeric'); return false;}
if (isNaN(parseFloat(f.second.value)))
{alert('second must be numeric'); return false;}
if (isNaN(parseFloat(f.total.value)))
{alert('total must be numeric'); return false;}
return true;
}
// end hiding -->
</script>
<h1>Hello
<script language="Javascript">
<!-- start hiding
now = new Date();
document.write("on " + now.toLocaleString());
// end hiding -->
</script>
</h1>
<form name="mainform" action="http://geneva.rutgers.edu/cgi/foo"
onSubmit="return checkform(this)">
First: <input type="text" name="first"
onChange="checknumeric(this)"><br>
Second: <input type="text" name="second" onChange="comptotal()"><br>
Total: <input type="text" name="total"><br>
<input type="submit">
</form>
</body>
</html>
PHP EXAMPLE
MAI N FORM
<html>
<h1>Demo of SGI serial number database</h1>
<h2>Display/Update Existing Contact</h2>
<FORM ACTION="/cgi-bin/php.cgi/~hedrick/updatecontact.php3" METHOD=POST>
Unix username: <INPUT TYPE="text" name="uname"><br>
<INPUT TYPE="submit">
</FORM>
</html>
UPDATECONTACT.PHP3
<html>
<h1>Demo of SGI serial number database</h1>
<h2>Display/Update Existing Contact</h2>
<?
$result = msql("sgidemo", "select * from contacts where uname = '$uname'");
if (msql_num_rows($result) <= 0) {
echo "<p>Sorry, we couldn't find $uname";
} else {
$uname = msql_result($result, 0, "uname");
$first = msql_result($result, 0, "first");
$last = msql_result($result, 0, "last");
$email = msql_result($result, 0, "email");
?>
<FORM ACTION="/cgi-bin/php.cgi/~hedrick/updatecontact2.php3"
METHOD=POST>
Unix username: <INPUT TYPE="text" name="uname" value="<?echo "$uname"?>"><br>
First name: <INPUT TYPE="text" name="first" value="<?echo "$first"?>"><br>
Last name: <INPUT TYPE="text" name="last" value="<?echo "$last"?>"><br>
Email address: <INPUT TYPE="text" name="email" value="<?echo "$email"?>"><br>
New password: <INPUT TYPE="password" name="npwd" value="<?echo "$npwd"?>"><br>
<p>
To make any change you must supply your existing password. If you
want to change your password, put the new password in the New Password
field.
<p>
Old password: <INPUT TYPE="password" name="opwd"><br>
<INPUT TYPE="submit">
</FORM>
<p>
<hr>
<h2>Administrator Only</h2>
<?
$result = msql_result($result, 0, "paid");
if ($result == "y" || $result == "Y") {
$pv = "on"; $ch = "checked";
} else {
$pv = "";
}
?>
<FORM ACTION="/cgi-bin/php.cgi/~hedrick/contactdel.php3" METHOD=POST>
Delete: <INPUT TYPE="checkbox" name="delete"><br>
Paid: <INPUT TYPE="checkbox" name="paid" <? echo "$ch" ?> ><br>
<INPUT TYPE="hidden" name=opaid value=<? echo "$pv" ?> >
Admin: <INPUT TYPE="text" name="admin"><br>
<INPUT TYPE="hidden" name=uname value="<? echo "$uname" ?>">
Password: <INPUT TYPE="password" name="pwd"><br>
<INPUT TYPE="submit">
</FORM>
<? } ?>
</html>
CONTACTDEL.PHP3
<html>
<h1>Demo of SGI serial number database</h1>
<h2>Admin change</h2>
<?
if ("$admin" != "hedrick") {
echo "<p>Sorry, you're not an admin";
} else {
$result = msql("sgidemo", "select * from contacts where uname = '$admin'");
$num = msql_numrows($result);
if ($num < 1) {
echo "<p>Sorry, can't find $admin";
} else if ($pwd != msql_result($result, 0, "pwd")) {
echo "<p>Sorry, wrong password";
} else if ($delete) {
$result = msql("sgidemo", "delete from contacts where uname = '$uname'");
if ($result > 0) {
echo "<p>Deleted $uname";
} else {
echo "<p>Couldn't delete $uname";
}
} else if ($paid != $opaid) {
if ($paid) {
$pv = "y"; $p = "paid";
} else {
$pv = ""; $p = "unpaid";
}
$result = msql("sgidemo", "update contacts set paid = '$pv' where uname =
'$uname'");
if ($result > 0) {
echo "<p>Updated $uname $p";
} else {
echo "<p>Couldn't update $uname";
}
}
}
?>
</html>
Download