PHP表单
PHP获取下拉菜单的数据
PHP下拉菜单单选
<?php //htmlspecialchars()函数把预定义的字符转换为HTML实体 $q = isset($_GET[‘q‘])? htmlspecialchars($_GET[‘q‘]) : ‘‘; if ($q) { if ($q == ‘Python‘) { echo ‘Python是一门脚本语言<br>http://....org‘; } else if ($q == ‘Java‘) { echo ‘Java是一门强类型静态语言‘; } else if ($q == ‘Go‘) { echo ‘Go是一门高并发的新兴语言<br>http://....org‘; } } else { ?> <form action="" method="get"> <select name="q"> <option value="">选择一个站点:</option> <option value="Python">Python:</option> <option value="Java">Java:</option> <option value="Go">Go:</option> </select> <input type="submit" value="提交"> </form> <?php } ?>
PHP下拉菜单多选
如果下拉菜单是多选的,可以通过将设置select name="q[]"以数组的方式获取
<?php //isset()检测变量是否设置并且不是NULL $q = isset($_POST[‘q‘]) ? $_POST[‘q‘] : ‘‘; if (is_array($q)) { $sites = array( ‘Python‘=>‘Python是一门动态语言‘, ‘Java‘=>‘Java是一门静态语言‘, ‘Go‘=>‘Go是一门新兴语言‘, ); foreach ($q as $val) { //PHP_EOL为常量,用于换行 echo $sites[$val] . PHP_EOL; } } else { ?> <form action="" method="post"> <select multiple="multiple" name="q[]"> <option value="">选择一个站点:</option> <option value="Python">Python</option> <option value="Java">Java</option> <option value="Go">Go</option> </select> <input type="submit" value="提交"> </form> <?php } ?>
单选按钮表单
<?php $q = isset($_GET[‘q‘]) ? htmlspecialchars($_GET[‘q‘]) : ‘‘; if ($q) { if ($q == ‘Python‘) { echo "Python---->haha"; } else if ($q == ‘Java‘) { echo "Java---->haha"; } else if ($q == ‘Go‘) { echo "Go---->haha"; } }else { ?> <form action="" method="get"> <input type="radio" name="q" value="Python" /> Python <input type="radio" name="q" value="Java" /> Java <input type="radio" name="q" value="Go" /> Go <input type="submit" value="提交"> </form> <?php } ?>
checkbox复选框
<?php $q = isset($_POST[‘q‘]) ? $_POST[‘q‘] : ‘‘; if (is_array($q)) { $sites = array( ‘Python‘ => ‘Python---->6666‘, ‘Java‘ => ‘Java---->6666‘, ‘Go‘ => ‘Go---->6666‘, ); foreach ($q as $val) { echo $sites[$val] . PHP_EOL; } } else { ?> <form action="" method="post"> <input type="checkbox" name="q[]" value="Python"> Python<br> <input type="checkbox" name="q[]" value="Java"> Java<br> <input type="checkbox" name="q[]" value="Go"> Go<br> <input type="submit" value="提交"> </form> <?php } ?>
PHP表单验证:验证客户端提交的表单数据
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <?php $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = test_input($_POST["name"]); $email = test_input($_POST["email"]); $website = test_input($_POST["website"]); $comment = test_input($_POST["comment"]); $gender = test_input($_POST["gender"]); } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>PHP 表单验证实例</h2> <form action=‘<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>‘ method="post"> 名字: <input type="text" name="name"> <br><br> E-mail: <input type="text" name="email"> <br><br> 网址: <input type="text" name="website"> <br><br> 备注: <textarea name="comment" rows="5" cols="40"></textarea> <br><br> 性别: <input type="radio" name="gender" value="female">女 <input type="radio" name="gender" value="male">男 <br><br> <input type="submit" name="submit" value="Submit"> </form> <?php echo "<h2>您输入的内容是:</h2>"; echo $name; echo ‘<br>‘; echo $email; echo ‘<br>‘; echo $website; echo ‘<br>‘; echo $comment; echo ‘<br>‘; echo $gender; ?> </body> </html>
PHP表单-必需字段
<?php //定义变量并默认设为空值 $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST[‘name‘])) { $nameErr = ‘名字是必需的。‘; } else { $name = test_input($_POST[‘name‘]); } if (empty($_POST[‘email‘])) { $emailErr = ‘邮箱是必需的。‘; } else { $email = test_input($_POST[‘email‘]); } if (empty($_POST[‘website‘])) { $website = ""; } else { $website = test_input($_POST[‘website‘]); } if (empty($_POST[‘comment‘])) { $comment = ‘‘; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST[‘gender‘])) { $genderErr = ‘性别是必需的。‘; } else { $gender = test_input($_POST["gender"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <!--显示错误信息--> <form method="post" action="<?php echo htmlspecialchars($_SERVER[‘PHP_SELF‘]);?>"> 名字:<input type="text" name="name"> <span class="error">* <?php echo $nameErr;?></span> <br><br> E-mail:<input type="text" name="email"> <span class="error">*<?php echo $emailErr;?></span> <br><br> 网址:<input type="text" name="website"> <span class="error"><?php echo $websiteErr;?></span> <br><br> 备注:<textarea name="comment" rows="5" cols="40"></textarea> <br><br> 性别: <input type="radio" name="gender" value="female">女 <input type="radio" name="gender" value="male">男 <span class="error">*<?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </form>
验证邮件和URL
<?php // 定义变量并默认设置为空值 $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = test_input($_POST["name"]); // 检测名字是否只包含字母跟空格 if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "只允许字母和空格"; } } if (empty($_POST["email"])) { $emailErr = "Email is required"; } else { $email = test_input($_POST["email"]); // 检测邮箱是否合法 if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = "非法邮箱格式"; } } if (empty($_POST["website"])) { $website = ""; } else { $website = test_input($_POST["website"]); // 检测 URL 地址是否合法 if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = "非法的 URL 的地址"; } } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["gender"])) { $genderErr = "性别是必需的"; } else { $gender = test_input($_POST["gender"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <form method="post" action="<?php echo htmlspecialchars($_SERVER[‘PHP_SELF‘]);?>"> 名字:<input type="text" name="name"> <span class="error">* <?php echo $nameErr;?></span> <br><br> E-mail:<input type="text" name="email"> <span class="error">*<?php echo $emailErr;?></span> <br><br> 网址:<input type="text" name="website"> <span class="error"><?php echo $websiteErr;?></span> <br><br> 备注:<textarea name="comment" rows="5" cols="40"></textarea> <br><br> 性别: <input type="radio" name="gender" value="female">女 <input type="radio" name="gender" value="male">男 <span class="error">*<?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </form>
PHP超级全局变量
PHP中预定义了几个超级全局变量,这意味着它们在一个脚本的全部作用域中都可用。不需要特别说明,就可以在函数及类中使用
PHP超级全局变量:
$GLOBALS-->是PHP的一个超级全局变量组,在一个PHP脚本的全部作用域中都可以访问。是一个包含了全部变量的全局组合数组。变量的名字就是数组的键
$_SERVER-->是一个包含了诸如头信息(header)、路径(path)、以及脚本位置(script locations)等等信息的数组。
$_SERVER[‘PHP_SELF‘]:当前执行脚本的文件名,与document root有关
$_REQUEST-->$_REQUEST用于收集HTML表单提交的数据,收集表单中的input字段数据。预定义的$_REQUEST变量包含了$_GET、$_POST和$_COOKIE的内容,$_REQUEST变量可用来收集通过GET和POST方法发送的表单数据
$_POST-->广泛应用于收集表单数据,收集表单中的input字段数据
$_GET-->收集表单中的input字段数据,预定义的$_GET变量用于收集来自method="get"的表单中的值
$_FILES
$_ENV
$_COOKIE
$_SESSION