Aims:
<form action="langscript.php" method="get"> <p> Which of these do you know? </p> <p> <input type="checkbox" name="langs[]" value="Groovy" id="groovyBox" /> <label for="groovyBox">Groovy</label> <input type="checkbox" name="langs[]" value="Java" id="javaBox" /> <label for="javaBox">Java</label> <input type="checkbox" name="langs[]" value="PHP" id="phpBox" /> <label for="phpBox">PHP</label> </p> <p> <input type="submit" value="Submit" />; </p> </form>
name
attributename
and value
will be passed to
the server for each box the user checksname
must end with []
<input type="checkbox" name="langs[]" value="Groovy" />
<input type="checkbox" name="langs[]" value="Java" />
<input type="checkbox" name="langs[]" value="PHP" />
$_GET['langs']
contains an indexed array,
not a string (hence $_GET
is an array that contains an array!)langscript.php
<?php if ( ! isset($_GET['langs']) ) { die('No languages! Are you stupid?'); } $length = count($_GET['langs']); echo "You know the following {$length} languages:"; echo '<ul>'; foreach ($_GET['langs'] as $lang) { echo "<li>$lang</li>"; } echo '</ul>'; ?>
langscript.php
$_GET
is an associative array$_GET['langs']
is itself an indexed array
echo '<ul>';
foreach ($_GET['langs'] as $lang)
{
echo "<li>$lang</li>";
}
echo '</ul>';
langscript.php
$_GET['langs']
has not been setisset
functiondie
function, outputs its argument and ends execution of
the script (but look at the source of the output!)count
function returns the length of the array<?php $output = ''; if ( isset($_GET['submitted']) ) { $celsius = ($_GET['fahr'] - 32) * 5/9; $output = "{$_GET['fahr']} Fahrenheit is $celsius Celsius"; } ?> <?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> <!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>Temperature conversion</title> <link rel="stylesheet" type="text/css" href="formlayout.css" /> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="get"> <p> <label for="fahrField">Fahrenheit:</label> <input type="text" name="fahr" id="fahrField" /> </p> <p> <input type="submit" name="submitted" value="Convert" /> </p> <p> <?php echo $output ?> </p> </form> </body> </html>
action
attribute$output
showing the
results of our temperature conversionform
, we echo the value of $output
name
attribute on the submit button:
<input type="submit" name="submitted" value="Convert" />
$_GET
if ( isset($_GET['submitted']) ) ...
If it is set, the user must have pressed submit, so this is not the first time
we are showing the form to the user
<?php $fahr = ''; $output = ''; if ( isset($_GET['submitted']) ) { $fahr = $_GET['fahr']; $celsius = ($fahr - 32) * 5/9; $output = "{$fahr} Fahrenheit is {$celsius} Celsius"; } ?>
<input type="text" name="fahr" id="fahrField" value="<?php echo $fahr; ?>" />
<?php $fahr = ''; $output = ''; if ( isset($_GET['submitted']) ) { $fahr = $_GET['fahr']; if ( ! is_numeric($fahr) ) { $output = "You must enter a numeric value"; } else { $celsius = ($fahr - 32) * 5/9; $output = "{$fahr} Fahrenheit is {$celsius} Celsius"; } } ?>