<?php
///////////////////////////////////////////////////////////
// example1.php - Example 1: select boxes using arrays
///////////////////////////////////////////////////////////
//
// eClasses.org class P100: Introduction to PHP
//
// Lesson no.  : 5
// Authors     : Gabriele Bartolini
// Type        : Assignment solution
// 
// Copyright (c) 2005-2006, Triche Osborne and Gabriele Bartolini
///////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////
// Control error reporting (display every error)
// Not to be used for real and public Web sites!
///////////////////////////////////////////////////////////
ini_set('display_errors'1);
error_reporting(E_ALL);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Exercise 1: select boxes using arrays</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link type="text/css" rel="stylesheet" href="../css/main.css" />

</head>
<body>

<div class="content">

<?php

// Detect empty $_POST array.
if ( !$_POST ) {
?>
    <h1>Favourite rock bands</h1>

    <form action="<?php echo $_SERVER['PHP_SELF'?>" method="POST">
    <fieldset>
    
        <label for="bands">Select at least a rock band (required):</label>
        <select name="bands[]" id="bands" multiple="multiple" size="3" />
            <option>The Beatles</option>
            <option>The Rolling Stones</option>
            <option>Creedence Clearwater Revival</option>
            <option>Led Zeppelin</option>
            <option>Lynyrd Skynyrd</option>
            <option>The Doors</option>
            <option>Jethro Tull</option>
            <option>Oasis</option>
            <option>Pink Floyd</option>
            <option>U2</option>
            <option>Dave Matthews Band</option>
        </select>

    </fieldset>
        <div id="formButtons">
            <input type="submit" value="Submit" class="submit" />
        </div>
    </fieldset>
    </form>

<?php

} else {

    
// Initialize processing alias.
    
$form $_POST;

    
//
    // ISSUE #1: NO CLEANING PROCEDURE IS PERFORMED
    //

    //
    // ISSUE #2: $form['bands'] is not checked if it is set
    //
    // if( isset($form['bands'])

    //
    // ISSUE #3: $form['bands'] is not checked if it is an array
    //
    //         && is_array($form['bands']) 

    //
    // ISSUE #4: $form['bands'] is not checked if it is empty
    //
    //         && !empty($form['bands']) ) {
    //    or
    //         && count($form['bands']) > 0) {

?>

    <h1>Your favourite bands are:</h1>
    <ul>
<?php
        
// Print each choice.
        
foreach ( $form['bands'] as $band ) {
?>
        <li><?php // ISSUE #5: No HTML preparation is performed
            
echo $band?></li>
<?php
        
}
?>
    </ul>
    <p>
        Thank you for participating. 
    </p>
<?php
    
/*
    }
    else {
?>
    <h1>You did not vote for any band.</h1>
    <p>Please go back and vote again.</p>
<?php
    }
    */
}
?>
</div>

</body>
</html>
<?php

///////////////////////////////////////////////////////////
// End of the exercise source
///////////////////////////////////////////////////////////
?>