一、数据提交
<form name="form1" action="zhuce.php" method="post"> </form>
action="zhuce.php" 转交给php文件获取表单数据
获取表单数据要使用预定义变量 $_POST
$_POST 数据类型为数组
//html文件:
<form name="form1" action="zhuce.php" method="post">
<input type="text" name="no">
<input type="text" name="name">
</form>
//php文件:
$no = $_POST['no'];
$name = $_POST['name'];
echo '学号:' . $no;
echo '<br>';
echo '姓名:' . $name;
二、防止非法访问php文件
empty — 检查一个变量是否为空,返回bool类型,如果为空则返回true
先判断$_POST是否为空,如果为空,则阻止非法访问。
<?php
if(empty($_POST)){
die('非法访问');
}
isset — 检查变量是否设置,并不是null isset($var,....);
如果$var存在并且不是null,则返回true,否则返回false
<?php
if(!isset($_POST['user'])){
die('非法访问');
}
三、页面跳转
(1)header方式
<?php
//如果$_POST是空数组则阻止非法访问
if(empty($_POST)){
//页面跳转
// header("location:register.html");
header("refresh:3;url=register.html");
echo '非法访问,等待跳转...';
}
(2)script方式
if(empty($_POST)){
echo '非法访问,等待跳转...';
echo <<<STR
<script>
setTimeout("window.location.href='register.html'",3000);
</script>
STR;
exit;
}
四、打印表单输入的数据
<?php
//阻止非法访问
if(empty($_POST)){
//页面跳转
// header("refresh:3;url=register.html");
echo '非法访问,等待跳转...';
}
//分别获取用户名和密码
$user = $_POST['user'];
$pwd = $_POST['pwd'];
echo "用户名{$user},密码{$pwd}";