<?php

///////////////////////////////////////////////////////////////////////////
//                        Re-usable Functions                            //
///////////////////////////////////////////////////////////////////////////

function drawSelect ($x) {
    global 
$f;
    
$X strtoupper($x);
    print 
"<label for=\"$x\">Field $X:</label>\n";
    print 
"<select name=\"$x\" id=\"$x\">\n";
    foreach (
$f[$x] as $o) {
        print 
"<option>$o</option>\n";
    }
    print 
"</select><br>\n";    
}

function 
drawHidden ($x) {
    
$X strtoupper($x);    
    print 
"<strong>Field $X:</strong> " $_POST[$x]; 
    print 
'<input type="hidden" name="' $x '" value="' $_POST[$x] . "\"><br>\n";
}

///////////////////////////////////////////////////////////////////////////
//                            HTML Header                                //
///////////////////////////////////////////////////////////////////////////


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<title>Choose Stuff</title>
<style type="text/css" media="screen,projection">
label, strong {
    font-weight:bold;
    font-style:none;
    color: black;
    background: inherit;
}
</style>
<h1>Choose Stuff</h1>
<?php

///////////////////////////////////////////////////////////////////////////
//                             Page Logic                                //
///////////////////////////////////////////////////////////////////////////


// if we've already chosen values A and B, then...
if ( isset($_POST['a']) && isset($_POST['b']) ) {

    
// ... say thank you.
    
print '<p>You chose ' $_POST['a'] . ' and ' .
          
$_POST['b'] . ". Now go away.</p>\n\n";

// otherwise...
} else {

    
// create a form for the visitor to choose stuff
    
print '<form action="choosestuff" method="post">' "\n";
    print 
"<fieldset>\n";
    print 
"<legend>Stuff that you can choose.</legend>\n";

    
// if we've already chosen value A, then...
    
if (isset($_POST['a'])) {

        
// define possible values of B depending on A.
        
if ($_POST['a'] == 'Fruits') {
            
$f['b'] = array('Apple','Banana','Mango');
        } else {
            
$f['b'] = array('London','Birmingham','Newark','Market Harborough','Leicester');
        }

        
// remember the value of A
        
drawHidden('a');
        
        
// ask the visitor to tell us B
        
drawSelect('b');

    
// otherwise...
    
} else {

        
// here are the possible values for A
        
$f['a'] = array('Fruits','Towns');
        
        
// ask the user to choose one
        
drawSelect('a');

    }

    print 
'<input type="submit">' "\n";
    print 
"</fieldset>\n";
    print 
"</form>\n";


    
?>