您的位置:首页 > 编程语言 > PHP开发

PHP学习笔记

2016-06-03 08:42 489 查看

表单

1. POST提交

<form method="post" action="send_simpleform.php"> //当提交后send_simpleform.php文件来处理提交数据,数据变量存放在全局变量$_POST中
<p><label for="user">Name:</label><br/>
<input type="text" id="user" name="user"></p>
<button type="sumbit" name="submit" value="send" >Send Message</button> //提交表单
</form>


1.1 获取表单输入数据

<p>Welcome,<?php echo $_POST['user']; ?></p>


提示:同样可以使用GET方法。POST可以处理比GET更多的数据,并且不会把数据传递到查询字符串中。如果使用了GET方法,确保将超全局变量修改为$_GET。

2. SELECT元素的HTML表单,列表选择元素

提示:SELECT列表一个或多个项目,html元素的name=名字+[],表示是数组,提交后数据存放在$_POST['products']的数组中。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="utf-8" />
<title>A simple HTML form</title>
</head>
<body>
<form method="post" action="send_simpleform.php">
<p><label for="user">Name:</label><br/>
<input type="text" id="user" name="user"></p>
<fieldset>
<legend>Select Some Products:</legend>
<input type="checkbox" id="tricorder"
name="products[]" value="Tricorder">
<label for="tricorder">Tricorder</label><br/>
<input type="checkbox" id="ORAC_AI"
name="products[]" value="ORAC_AT">
<label for="ORAC_AI">ORAC_AI</label><br/>
<input type="checkbox" id="HAL_2000"
name="products[]" value="HAL_2000">
<label for="HAL_2000">HAL_2000</label>
</fieldset>
<br/>
<button type="submit" name="submit" value="send">Send Message</button>
</form>
</body>
</html>


2.1 遍历选择结果

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Name:
<?php
echo $_POST['user'];
?>
</p>

<p>Your product choices are:
<?php
if(!empty($_POST['products'])){ //检测当不为空时
echo "<ul>";
foreach($_POST['products'] as $value){
echo "<li>$value</li>";
}
echo "</ul>";
}
else{
echo "None";
}

?>
</p>
</body>
</html>


3. HTML自身处理表单,即HTML和PHP代码混合在一个文件中

<!DOCTYPE html>
<html>
<head>
<title>A PHP number guessing script</title>
</head>
<body>
<?php
$num_to_guess=42;
if(!isset($_POST['guess'])){ //测试变量是否赋值
$message="Welcom to the guessing machine!";
}elseif(!is_numeric($_POST['guess'])){ //测试变量是否为数字
$message="I dot't understand that response.";
}elseif($_POST['guess']==$num_to_guess){
$message="Well done!";
}
?>
<h1><?php echo $message; ?> </h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <!--提交到自身,即重新加载当前页。-->
<p><label for="guess">Type your guess here:</label><br/>
<input type="text" id="guess" name="guess"/></p>
<button type="submit" name="submit" value="submit">Submit</button>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: