Blogger Information
Blog 14
fans 0
comment 0
visits 8791
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
一个前端后端的综合实例
于星辉
Original
706 people have browsed it

一、前端页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>注册</title>
  8. </head>
  9. <body>
  10. <h2>用户注册</h2>
  11. <table>
  12. <tr>
  13. <td>账户</td>
  14. <td><input type="text" name="username"></td>
  15. <td><span id="err_msg"></span></td>
  16. </tr>
  17. <tr>
  18. <td>密码</td>
  19. <td><input type="password" name="password"></td>
  20. </tr>
  21. <tr>
  22. <td>性别</td>
  23. <td>
  24. <input type="radio" name="gender" value ="1" checked>
  25. <input type="radio" name ="gender" value="2">
  26. </td>
  27. </tr>
  28. <tr align = "center">
  29. <td colspan= "2">
  30. <input type="button" name ="btn" value ="确定">
  31. </td>
  32. </tr>
  33. </table>
  34. <script type="text/javascript" src="https://cdn.bootcss/jquery/3.3.1/jquery.min.js"></script>
  35. <script type="text/javascript">
  36. //检测用户名是否重复
  37. $('input[name="username"]').blur(function(){
  38. var data = {};
  39. data.username = $('input[name="username"]').val();
  40. data.type= 1;
  41. if (res.status==0)
  42. {
  43. $("#err_msg").text(res.msg);
  44. $("#err_msg").css('color','red');
  45. $('input[name="password"]').attr('disabled','disabled');
  46. }else{
  47. $("#err_msg").text(res.msg);
  48. $("#err_msg").css('color','green');
  49. $('input[name="password"]').removeAttr('disabled');
  50. }
  51. },'json')
  52. //检查用户名和密码不能为空
  53. $('input[name="btn"]').click(function(){
  54. var data = {};
  55. data.username = $('input[name="usrname"]').val();
  56. data.password = $('input[name ="password"]').val();
  57. data.gender = $('input[name="gender"]').val();
  58. data.type = 2;
  59. if(data.username =='' || data.password == '')
  60. {
  61. alert('用户名或密码不能为空');
  62. return;
  63. }
  64. $.post('3-doSubmit.php',data,function(res){
  65. if(res.status == 1)
  66. {
  67. alert(res.msg);
  68. }else{
  69. alert(res.msg);
  70. }
  71. },'json')
  72. })
  73. </script>
  74. </body>
  75. </html>

二、 数据库连接

  1. <?php
  2. namespace connect_chloe;
  3. use PDO;
  4. $config = require __DIR__.'\\config\database.php';
  5. extract($config);
  6. // dsn data source name 包括PDO驱动名称 主机名 端口号 数据库名称
  7. $dsn = sprintf('%s:host=%s;port=%d;dbname=%s',$type,$host,$port,$dbname);
  8. try{
  9. $pdo= new PDO($dsn,$username,$password,[PDO::ATTR_ERRMODE=>PDO::ERRMODE_WARINING]);
  10. $pdo->setAtrribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC0);
  11. }catch(\Exception $e){
  12. die('Connection error:'.$e->getMessage());
  13. }
  14. ?>

三、

  1. <?php
  2. namespace pdo_chloe;
  3. require 'common.php';
  4. //接收数据
  5. $username = !empty($_POST['username']) && isset($_POST['username']) ? $_POST['username'] :null;
  6. $password =!empty($_POST['password']) && isset($_POST['password']) ? $_POST['password'] : null;
  7. $gender = !empty($_POST['$gender']) && isset($_POST['$gender']) ? $_POST['$gender'] : null;
  8. $create_time = time();
  9. //请求分发器 type 1 检查用户名第一名 2 注册 3 登录
  10. $type = isset($_POST['type']) && !empty($_POST['type']) ? intval($_POST['type']) : null;
  11. switch($type){
  12. case 1:
  13. $res = checkUname($username);
  14. if($res){
  15. echo json_encode(['status'=>0,'msg'=>'用户名已被占用'],320);
  16. }else{
  17. echo json_encode(['status'=>1,'msg'=>'用户名合法'],320);
  18. }
  19. break;
  20. case 2:
  21. $sql = "INSERT INTO `user` = '{$username}',`pwd`='{$password}',`gender`={$gender},`create_time`={$create_time};";
  22. if(empty($username) || empty($password) || empty($gender))
  23. {
  24. echo json_encode(['status'=>10,'msg'=>'系统字段错误']);
  25. }else{
  26. $flag = checkUname($username);
  27. if(!$flag)
  28. {
  29. $res = $pdo->exec($sql);
  30. if($res)
  31. {
  32. echo json_encode(['status'=>1,'msg'=>'注册成功'],320);
  33. }else{
  34. echo json_encode(['status'=>0,'msg'=>'注册失败'],320);
  35. }
  36. }else{
  37. echo json_encode(['status'=>0,'msg'=>'请勿重复提交'],320);
  38. }
  39. }
  40. break;
  41. default:
  42. break;
  43. }
  44. ?>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post