Workshop in Computational Bioskills - Spring 2005 Lesson 9 - PHP Advanced (1) Making Web Forms Handling File System
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>
if ( $_GET['_submit_check'] == 1 ) { $form_errors = validate_form(); if ($form_errors != '') { show_form($form_errors); } else { process_form(); } } else { show_form(); }
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
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.
mode | action | starts from | file does not exist |
rb | read | file beginning | return false |
rb+ | read,write | file beginning | return false |
wb | write | file beginning | create it |
wb+ | read,write | file beginning | create it |
ab | write | end of file | create it |
ab+ | read,write | end of file | create it |
xb | write | file beginning | create it, if exists return false |
xb+ | read,write | file beginning | create it, if exists return false |
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">File upload.php handle the file. PHP stores all the uploaded file information in the $_FILES autoglobal array.Choose a file to upload: <input name="userfile" type="file" /> <input type="submit" enctype='multipart/form-data' value="Upload File" /> </form>