PHP与Web数据交互
表单提交到p1.php,获取前端输入的数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Author" content=" ">
<title>Document</title>
</head>
<body>
<div></div>
<form action="p1.php" method="post">
<p>账号:
<input type="text" name="u_id" id="u_id"/></p>
<p>密码:
<input type="password" name="u_pass" id="u_pass"/></p>
<p>描述:
<textarea name="u_about" id="u_about" cols="45" rows="5"></textarea><br/>
<input type="submit" value="submit" name="send" id="button">
</form>
</body>
</html>
编写完之后,把html后缀改为php,这里命名为p10.php,接着编写p1.php的页面,
p10.php表单提交到p1.php
<?php
if(isset($_POST['send'])){
$u = $_POST['u_id'];
$p = $_POST['u_pass'];
$d = $_POST['u_about'];
echo "你的账号是".$u."<br>";
echo "你的密码是".$p."<br>";
echo "你的描述是".$d."<br>";
}
?>
访问p10.php界面
输入账号:gerry
输入密码:123456
输入描述:哈哈哈哈
点击submit
提交到p1.php,结果是这样的
你的账号是gerry
你的密码是123456
你的描述是哈哈哈哈
下拉列表的获取
接着我们在上面的php基础上再建立一个php文件,命名p11.php,代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Author" content=" ">
<title>Document</title>
</head>
<body>
<div></div>
<form action="p2.php" method="post">
<p>账号:
<input type="text" name="u_id" id="u_id"/></p>
<p>密码:
<input type="password" name="u_pass" id="u_pass"/></p>
学历:<select name="edu" id="edu">
<option value="博士">博士</option>
<option value="硕士">硕士</option>
<option value="本科">本科</option>
<option value="大专">大专</option>
</select><br/><br/>
<input type="submit" value="submit" name="send" id="button">
</form>
</body>
</html>
建立p2.php,代码为:
<?php
if(isset($_POST['send'])){
$u = $_POST['u_id'];
$p = $_POST['u_pass'];
$edu = $_POST['edu'];
echo "你的账号是".$u."<br>";
echo "你的密码是".$p."<br>";
echo "学历是".$edu."<br>";
// echo "你的描述是".$d."<br>";
}
?>
checkbox复选框
p11.php代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Author" content=" ">
<title>Document</title>
</head>
<body>
<div></div>
<form action="p2.php" method="post">
<p>账号:
<input type="text" name="u_id" id="u_id"/></p>
<p>密码:
<input type="password" name="u_pass" id="u_pass"/></p>
学历:<select name="edu" id="edu">
<option value="博士">博士</option>
<option value="硕士">硕士</option>
<option value="本科">本科</option>
<option value="大专">大专</option>
</select><br/><br/>
性别:
<label><input type="radio" name="sex" value="男" id="sex_0">男</label>
<label><input type="radio" name="sex" value="女" id="sex_1">女</label><br/><br/>
<input type="checkbox" name="inte[]" value="读书" id="inte_0" />读书
<input type="checkbox" name="inte[]" value="跑步" id="inte_1" />跑步
<input type="checkbox" name="inte[]" value="音乐" id="inte_2" />音乐
<input type="checkbox" name="inte[]" value="书法" id="inte_3" />书法<br/><br/>
<input type="submit" value="submit" name="send" id="button">
</form>
</body>
</html>
p2.php代码如下:
<?php
if(isset($_POST['send'])){
$u = $_POST['u_id'];
$p = $_POST['u_pass'];
$edu = $_POST['edu'];
$sex = $_POST['sex'];
$cb = $_POST['inte'];
echo "你的账号是".$u."<br>";
echo "你的密码是".$p."<br>";
echo "学历是".$edu."<br>";
echo "性别:".$sex."<br>";
echo "兴趣爱好:";
foreach ($cb as $i) {
echo $i." ,";
}
// echo "你的描述是".$d."<br>";
}
?>
本课所要做的练习 上图
register.php代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Author" content=" ">
<title>Document</title>
</head>
<body>
<div></div>
<form action="register_ok.php" method="post">
<p>姓名:
<input type="text" name="u_id" id="u_id"/></p>
<p>密码:
<input type="password" name="u_pass" id="u_pass"/></p>
性别:
<label><input type="radio" name="sex" value="男" id="sex_0">男</label>
<label><input type="radio" name="sex" value="女" id="sex_1">女</label><br/><br/>
学历:<select name="edu" id="edu">
<option value="小学">小学</option>
<option value="初中">初中</option>
<option value="高中/中专">高中/中专</option>
<option value="本科/大专">本科/大专</option>
</select><br/><br/>
爱好:
<input type="checkbox" name="inte[]" value="音乐" id="inte_2" />音乐
<input type="checkbox" name="inte[]" value="书法" id="inte_3" />书法
<input type="checkbox" name="inte[]" value="读书" id="inte_0" />运动
<input type="checkbox" name="inte[]" value="跑步" id="inte_1" />旅游
<br/><br/>
图片:<input name="imgfile" type="file" accept="image/gif, image/jpeg"/><br/><br/>
<!-- <input name="upload" type="submit" value="上传" /> -->
个人简历:<br/>
<textarea name="u_about" id="u_about" cols="45" rows="5"></textarea><br/>
<input type="submit" value="提交" name="send" id="button">
<input type="reset" value="重置" name="reset" id="reset">
</form>
</body>
</html>
register_ok.php代码如下:
<?php
if(isset($_POST['send'])){
$u = $_POST['u_id'];//
$p = $_POST['u_pass'];//
$sex = $_POST['sex'];//
$edu = $_POST['edu'];//
$cb = $_POST['inte'];//
$image = $_POST['imgfile'];
$txt = $_POST['u_about'];
// echo "你的描述是".$d."<br>";
echo "姓名:".$u."<br>";
echo "密码:".$p."<br>";
echo "性别:".$sex."<br>";
echo "学历:".$edu."<br>";
echo "爱好:";
foreach ($cb as $i) {
echo $i." ,";
}
echo "<br>";
echo "图片:".$image."<br>";
echo "个人简历:".$txt;
}
?>
register.php页面效果如下:
输出效果如下:
以上就是课上所做的笔记