CSSE 290 Web Programming

Lecture 10: Submitting Form Data; POST; Debugging

Reading: 6.3 - 6.5

Attribution:Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.

Otherwise noted: Claude Anderson was given permission to modify the slides for CSSE 290 at Rose-Hulman by author Jessica Miller. The authors' original slides, based on Web Programming Step by Step, can be seen at http://webstepbook.com.
Some of the examples in some days' slides are from David Fisher at Rose-Hulman, who was kind enough to allow me to use them. My intention is to mark these examples with [DSF].

Valid HTML Valid CSS!

Announcements

6.4: Processing Form Data in PHP

Recap: Submitting Data to a Web Server

Recap: HTTP GET vs. POST requests

Form POST example

<form action="http://foo.com/app.php" method="post">
	<div>
		Name: <input type="text" name="name" /> <br />
		Food: <input type="text" name="meal" /> <br />
		<label>Meat? <input type="checkbox" name="meat" /></label> <br />
		<input type="submit" />
	<div>
</form>

PHP: GET or POST?

if ($_SERVER["REQUEST_METHOD"] == "GET") {
	# Process a GET request
	...
} elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
	# Process a POST request
	...
}

Recap: The htmlspecialchars function

htmlspecialchars returns an HTML-escaped version of a string
$text = "<p>hi 2 u & me</p>";
$text = htmlspecialchars($text);   # "&lt;p&gt;hi 2 u &amp; me&lt;/p&gt;"

"Superglobal" arrays

Associative Arrays

$blackbook = array();
$blackbook["marty"] = "444-444-4444";
$blackbook["stuart"] = "444-444-5555";
...
print "Marty's number is " . $blackbook["marty"] . ".\n";

Uploading Files

<form action="http://webster.cs.washington.edu/params.php"
      method="post" enctype="multipart/form-data">
	Upload an image as your avatar:
	<input type="file" name="avatar" />
	<input type="submit" />
</form>

PHP: Processing an Uploaded File

Uploading details

<input type="file" name="avatar" />

Processing uploaded file, example

$username = $_POST["username"];
if (is_uploaded_file($_FILES["avatar"]["tmp_name"])) {
	move_uploaded_file($_FILES["avatar"]["tmp_name"], "$username/avatar.jpg");
	print "Saved uploaded file as $username/avatar.jpg\n";
} else {
	print "Error: required file not uploaded";
}

Including PHP Files in Code: include

include("filename");
include("header.html");
include("shared-code.php");

More Associative Arrays: Creation

$name = array();
$name["key"] = value;
...
$name["key"] = value;
$name = array(key => value, ..., key => value);
$date_hired = array("Cary"   => "1981",
					"Lynn"   => "1987",
                    "Claude" => "1988",
                    "David"  => "1994");

Associative Array Functions

if (isset($date_hired["Lynn"])) {
	print "Lynns's hire date is {$date_hired['Lynn']}\n";
} else {
	print "No hire date found for Lynn\n";
}
name(s) category
isset, array_key_exists whether the array contains value for given key
array_keys, array_values an array containing all keys or all values in the assoc.array
asort, arsort sorts by value, in normal or reverse order
ksort, krsort sorts by key, in normal or reverse order

Note: isset()does not return TRUE for an array key whose corresponding value is NULL; array_key_exists() does.

foreach Loop and Associative Arrays

foreach ($hire_date as $name => $year) {
	print "$name was hired in $year\n";
}
Cary was hired in 1981
Lynn was hired in 1987
Claude was hired in 1988
David was hired in 1994

PHP Debugging Setup (slide 1)

interpreter configuration
  1. Assumption: You already have a PHP server configured in Aptana, and have XDebug set up. If not, just watch for now; after the demo get help and catch up!
  2. In Eclipse, Window → Preferences → Aptana Studio → Editors → PHP → PHP Interpreter. If you already have an interpreter, click Edit; otherwise, click New
  3. Fill out the form as in the picture (the value in the Name field can be different)
  4. Extract loop.php from loop.zip and place it in a new or existing PHP project in Aptana.

Continued on next slide

A PHP Debugging Setup (slide 2)

interpreter configuration
  1. In Eclipse, Window → Preferences → Aptana Studio → Editors → PHP → Debug
  2. Make it look like this picture (the value in the Name field can be different)
  3. In C:\xampp\php\php.ini, find the xdebug section, and add a line:
          xdebug.remote_port=9000

Next we will set a breakpoint, Debug as Script, and Debug as Server

Exercise: Pair programming (finish on Thursday?)

Initial state:

no messages

Exercise: Pair programming (finish on Thursday?)

After inserting message with picture:

no messages

Click here to get starting code

Anonymous message (note: latest message is first):

no messages