<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <form action="test.php" method="post"> <fieldset> <legend>Enter your information in the form below:</legend> <p><label>Name:<input name="name" type="text" size="20" maxlength="40" /></label></p> <p><label>Email Address:<input name="email" type="text" size="40" maxlength="60" /></label></p> <p> <label for="gender">Gender: </label> <input type="radio" name="gender" value="M" /> Male <input type="radio" name="gender" value="F" /> Female </p> <p> <input type="checkbox" name="interests[]" value="Music" />Music <input type="checkbox" name="interests[]" value="Movies" />Movies <input type="checkbox" name="interests[]" value="Books" />Books </p> <p> <label> Age: <select name="age"> <option value="0-29">Under 30 </option> <option value="30-60">Between 30 and 60</option> <option value="60+">Over 60 </option> </select> </label> </p> <p><label>Comments: <textarea name="comments" rows="3" cols="40"></textarea></label></p> </fieldset> <p><input type="submit" name="submit" value="Submit My Information" /></p> </form> <?php $name = $_REQUEST['name']; $email = $_REQUEST['email']; $comments = $_REQUEST['comments']; /* * 点击submit,跳转action * 第一次进来,输入框没有内容,echo自然是空的 * 输入内容,点击submit,跳转test.php,相当于刷新了一遍当前页面,echo输入的值 */ echo "<p>Thank you, $name, for the following comments:<br /> $comments We will reply to you at $email.</p>"; if (isset($_REQUEST['gender'])) { // isset 用于检测一个变量是否被设置 $gender = $_REQUEST['gender']; } else { $gender = NULL; } if ($gender == 'M') { echo '<p>Good day, Sir!</p>'; } elseif ($gender == 'F') { echo '<p>Good day, Madam!</p>'; } else { echo '<p>You forgot to enter your gender!</p>'; } switch ($gender) { case 'M': echo '<p>Good day, Sir!</p>'; break; case 'F': echo '<p>Good day, Madam!</p>'; break; default: echo '<p>You forgot to enter your gender!</p>'; break; } /* * isset() 它用于测试一个变量是否具有值(包括0、FALSE,或者一个空字符串,但不能是NULL) * 使用isset()函数的一个问题是:空字符串测试为TRUE * empty() 它将检查一个变量是否具有空(empty)值:空字符串、0、NULL或FALSE * 表单验证的第一个目标是确保在表单元素中输入或选择了某些内容 * 第二个目标是确保提交的数据具有正确的类型(数字、字符串等)、 * 正确的格式(如电子邮件地址)或特定的可接受的值(如$gender应该等于M或F) */ if (!empty($_REQUEST['name'])) { $name = $_REQUEST['name']; } else { $name = NULL; echo '<p class="error">You forgot to enter your name!</p>'; } /* * 如果恶意用户创建他们自己的表单并提交给handle_form.php脚本, * 他们可以给$_REQUEST['gender']提供他们想要的任何值 * 在这个脚本的前一个版本中,通过引用$_REQUEST数组来访问值。 * 但是,由于这些变量来自于一个使用post方法的表单, * $_POST将是更准确(因而更安全)的引用 * 如果数组使用字符串作为键,用于括住键的引号将使语法变得混乱。 * 为了解决这个问题,把数组名和键包括在花括号中 * {$_POST['name']} */ $band[] = 'Jemaine'; $band[] = 'Bret'; $band[] = 'Murray'; $band['fan'] = 'Mel'; $band['fan'] = 'Dave'; // 重写 $fruit[2] = 'orange'; $states = array('IA' => 'Iowa', 'MD' => 'Maryland'); $artists = array('Clem Snide', 'Shins', 'Eels'); $artists = array(1 => 'Clem Snide', 'Shins', 'Eels'); $artists = range(1, 10); echo $artists[0]; foreach ($artists as $value) { echo "The value at $value"; } foreach ($artists as $key => $value) { echo "The value at $key is $value"; } $num = count($artists); // 创建连续字母的数组 $alphabet = range('a', 'z'); echo is_array($alphabet); // 多维数组 $mexico = array( 'YU' => 'Yucatan', 'BC' => 'Baja California' ); $us = array( 'MD' => 'Maryland', 'IL' => 'Illinois' ); $n_america = array( 'Mexico' => $mexico, 'United States' => $us ); foreach ($n_america as $country => $list) { echo "<h2>$country</h2><ul>"; foreach ($list as $k => $v) { echo "<li>$k - $v</li>"; } echo "</ul>"; } if ($_POST['submit']) { echo $_POST['interests'][1]; } /* * sort 重置数组的键 * asort 维持键 * ksort 按键对数组排序 * rsort * arsort * krsort */ ?> </body> </html>