Thursday, April 3, 2008

Week 10

Post your homework to the comments of this post!

Form Processing with PHP


In order to process the information submitted by a user to a form on your site, you need a way to capture that data. Because HTML, CSS, and JavaScript all happen on the client-side, and can’t send information back to the browser, you need something like PHP, working on the server side, to do this. We’re going to go over two very simple form processing scripts today.


The first example is for sending the results of a form as an email.


Sample Files: (control+click or right click to download)




Step 1, Set the Form Action


To invoke the PHP code you are going to use to process the form, you are going to submit the form to a .php page. This is accomplished by setting the form action to the name of the page:


<form action=”email_form.php” method=”post”>


Step 2, Create the handler script


Next, you are going to create a .php file to process the form contents. In this example, we are going to name this script email_form.php


By submitting our form to this script, the values from the form are passed to it. These values are automatically stored in an array called $_POST. We can now access the values in this array, and the first thing we need to do is to make them easier to work with:

//Change post variables to regular variables


$ipod = $_POST['ipod'];


$laptop = $_POST['laptop'];

$cell_phone = $_POST['cell_phone'];

$borough = $_POST['borough'];


Note that the values between the brackets [’…’] match the names of the inputs you passed from your form.


The next thing you need to do is turn all these values into a single string so it can be emailed:


//Turn all the variables into a single string.

//Note: .= appends to the current variable value rather than replacing it


$formresults = "Ipod: ".$ipod."\n";


$formresults .= "Laptop: ".$laptop."\n";

$formresults .= "Cellphone: ".$cell_phone."\n";

$formresults .= "Borough: ".$borough;


Next, you will use the built-in function called mail to email the form results to the designated email:


//send values as email

mail("youremail@domain.com", "Form Results from Web site", $formresults, "From: youremailhere@yourdomain.com");


Now, it’s good practice to give your visitors a confirmation message so they know their information was submitted:

//print opening html

print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"


\"http://www.w3.org/TR/html4/loose.dtd\">

<html>

<head>

<title>Form Example</title>

<meta name=\"generator\" content=\"BBEdit 8.2\">

</head>

<body>");



//Print confirmation message to the screen

print("Your answers have been emailed to our secret headquarters. Thanks for playing!");


//print closing html

print("</body>

</html>");


Last, don’t forget to close your php tag:

?>


Saving Form Results to a File



Say instead of emailing your results you just want to store them in a file on your server. This example shows you how.


Sample Files: (control+click or right click to download)



Your form file remains the same, but for the purpose of this example, we are going to change the action to this:

<form action=”file_form.php” method=”post”>


This time we are processing the form with a php script called file_form.php



The beginning of this script is identical to the previous example.


//Change post variables to regular variables


$ipod = $_POST['ipod'];

$laptop = $_POST['laptop'];

$cell_phone = $_POST['cell_phone'];

$borough = $_POST['borough'];


//Turn all the variables into a single string.

//Note: .= appends to the current variable value rather than replacing it


$formresults = "Ipod: ".$ipod."\n>";


$formresults .= "Laptop: ".$laptop."\n";

$formresults .= "Cellphone: ".$cell_phone."\n";

$formresults .= "Borough: ".$borough;


This time, however, you are going to use a number of functions that are designed to handle opening, writing, and closing files on the server.


//this code is adapted from Example 1 at http://us3.php.net/manual/en/function.fwrite.php


//identify the file

$filename = "formdata.txt";


//see if the file exists and is writable




// Let's make sure the file exists and is writable first.

if (is_writable($filename)) {


// the function fopen opens the specified file. The 'a' argument opens the file in append mode


if (!$handle = fopen($filename, 'a')) {

echo "Cannot open file ($filename)";

exit;

}


// Write $file to our opened file.

if (fwrite($handle, $formresults) === FALSE) {


echo "Cannot write to file ($filename)";

exit;

}


//close the file

fclose($handle);


} else {

echo "The file $filename is not writable";

exit;

}



The last thing you need to do is create the text file. Open up a text editor and save a blank document as formdata.txt. Upload it to your site. You may need change the file permissions in order for the script to work right. The permissions should be 666, readable and writeable by everyone.



5 comments:

Yelena M. said...

http://a.parsons.edu/~ymirchevskaya/chernova/portfolio.html

Dangerous Chris said...

skimmmilk.com/skimmhome.html

Ara Ko said...

Ara Ko
a.parsons.edu/~arko/hwpage

Andre Vandenburg said...

http://a.parsons.edu/~avandenburg/cosiweb/

http://a.parsons.edu/~avandenburg/cosiweb/bio.html

Pack 21 said...

http://a.parsons.edu/~nhicks/pack21