PHP第二十课1:php与web页面交互
html:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action="zhangshuai.php" method="post"> <p> <!--input:t单行文本框--> 用户名:<input type="text" name="uname" id="uname"> </p> <p> <!--input:p密码框--> 密 码:<input type="password" name="pwd" id="pwd"> </p> <p> <!--input:s--> <input type="submit" value="登录"> <!--input:reset--> <input type="reset" value="重置"> </p> </form> </body> </html>
php:
<?php header("content-type:text/html;charset=utf-8"); //打印函数 function zs($name){ echo "<pre style='border: 1px solid #333; border-radius:8px;padding: 10px;'>"; print_r($name); echo "</pre>"; } //调用函数 zs($_POST); ?>
当用户录入数据的时候,我们要对用户录入的数据,进行校验,通过javascript脚本实现:
<!--脚本标签--> <script type="text/javascript"> function check(){ //抓取元素 var uname = document.getElementById("uname"); var pwd = document.getElementById("pwd"); console.log(uname); console.log(pwd) if(uname.value==""){ alert("用户名不能为空!"); return false; } if(pwd.value==""){ alert("密码不能为空!"); return false; } return true; } </script>
