一些输入的示例,脚本顶部的监视器应该为其他输入提供线索:
<?php
/*monitor the POST request, delete in production*/
echo "<pre>";
print_r($_POST);
echo "</pre>";
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
//check things like captcha / empty fields first,
//if something fails: fill variables
$postedText = isset($_POST['textInput']) ? $_POST['textInput'] : null;
$postedCheckbox = isset($_POST['checkboxInput']) ? 1 : 0;
$postedRadio = isset($_POST['radioInput']) ? $_POST['radioInput'] : null;
$postedSelect = $_POST['selectInput'];
$postedTextarea = isset($_POST['textareaInput']) ? $_POST['textareaInput'] : null;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form method="post">
<label for="textInput">Name:</label>
<input type="text"
id="textInput"
name="textInput"
value="<?php echo (isset($postedText) ? $postedText : 'text') ;?>"
/>
<br />
<label for="checkboxInput">Checkbox:</label>
<input type="checkbox"
id="checkboxInput"
name="checkboxInput"
<?php echo (isset($postedCheckbox) && $postedCheckbox === 1 ? "checked='checked'" : '') ;?>
/>
<br />
<label for="rbInput">Radio:</label>
<input type="radio"
id="rbInputMale"
name="rbInput"
value="male"
<?php echo (isset($postedRadio) && $postedRadio === 'male' ? "checked='checked'" : '') ;?>
/> male
<input type="radio"
id="rbInputFemale"
name="rbInput"
value="female"
<?php echo (isset($postedRadio) && $postedRadio === 'female' ? "checked='checked'" : '') ;?>
/> female
<br />
<label for="selectInput">Select</label>
<select name="selectInput"
id="selectInput">
<?php
$options = array(
'one' => 'one',
'two' => 'two',
'three' => 'three'
);
foreach ($options as $key => $value)
{
?>
<option value="<?php echo $value;?>"
<?php
if(isset($postedSelect))
{
echo ($value === $postedSelect ? "selected='selected'" : '');
}
?>
>
<?php echo $key;?>
</option>
<?php
}
?>
</select>
<br />
<label for="textareaInput">textarea</label>
<textarea name="textareaInput" id="textareaInput"><?php echo (isset($postedTextarea) ? $postedTextarea : '');?></textarea>
<br />
<input type="submit" value="submit" />
</form>
</body>
</html>