Workshop in Computational Bioskills - Lesson 9

Workshop in Computational Bioskills - Spring 2005

Lesson 9 - PHP Advanced (1)

Making Web Forms
Handling File System

Making Web Forms
Web forms are essential in almost any dynamic web application. We saw how to write a
form in HTML, and here we'll see how to process the user input.


Let's start with an example we already saw:

simple_form.html:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
  <form action="action.php" method="get">
   <p>Your name: <input type="text" name="my_name"> </p>
   <p>Your age:  <input type="text" name="my_age"> </p>
   <p><input type="submit" ></p>
  </form>
 </body>
</html>


The user fills the 2 windows, and the script action.php is 
called. The result is displayed on the browser: 

<html>
 <head>
  <title>PHP Action</title>
 </head>
 <body>
  Hi <?php echo $_GET['my_name']; ?>.
  You are <?php echo $_GET['my_age']; ?> years old.
 </body>
</html>


Can we do it in a single php file? simple_form.php: <html> <head> <title>PHP Form</title> </head> <body> <?php if array_key_exists('my_name',$_GET) { print "Hi ".$_GET['my_name']; print "You are ".$_GET['my_age']." years old."; } else { print<<< _HTML_ <form method="get" action="$_SERVER[PHP_SELF]"> <p>Your name: <input type="text" name="my_name" /></p> <p>Your age: <input type="text" name="my_age" /></p> <p><input type="submit" /></p> </form> _HTML_; } ?> </body> </html>

Input Validation

if ( $_GET['_submit_check'] == 1 ) 
{ 
    $form_errors = validate_form();
    if ($form_errors != '') {
        show_form($form_errors);
    } else {
        process_form();
    }
}
else {
    show_form();
}

A simple check:


function validate_form() 
{
$error = '';
if (strlen( trim($_POST['product'])) < 1 ) 
  {
    $error = 'Please enter a product name';
  }

if ($_POST['price'] != strval(floatval($_POST['price'])))
  {
    $error = 'Please enter a valid price.';
  }
return $error;
}


Run valid_price.php


<select> Menus

o Displays a list of options (option to select one item or several items)


<form method="get" action="order.php">
<select name="order">
<option>Book</option>
<option>Ball</option>
<option>Car</option>
</select>
</form>

o To validate it, just check that the given value is one of the options. o Run valid_list.php o valid_multiple_list.php enables the choice of multiple items.
Displaying default parameters
o With HTML we can display a default parameters in the form example. o PHP you can just print these default options to the HTML file.

File System Permission code: specifies read/write permissions:
modeactionstarts fromfile does not exist
rbreadfile beginningreturn false
rb+read,writefile beginningreturn false
wbwritefile beginningcreate it
wb+read,writefile beginningcreate it
abwriteend of filecreate it
ab+read,writeend of filecreate it
xbwritefile beginningcreate it, if exists return false
xb+read,writefile beginningcreate it, if exists return false

To check permissions use file_exists($file_name), is_readable($file_name), is_writable($file_name).
Uploading a file

o Uploading file from the user file system to the server.
o Basically it's just another type of form.
o Possible in CS only under a special secured directory.


     <form action="upload.php" method="post">
     

Choose a file to upload: <input name="userfile" type="file" /> <input type="submit" enctype='multipart/form-data' value="Upload File" /> </form>

File upload.php handle the file. PHP stores all the uploaded file information in the $_FILES autoglobal array.