Blogger Information
Blog 37
fans 1
comment 0
visits 27111
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1225_jquery异步请求 第40课
叮叮当当
Original
735 people have browsed it

1、登录页 form.html

  1. <form>
  2. <input type="text" name="username" placeholder="用户名">
  3. <input type="password" name="pwd" placeholder="密码">
  4. <select name="city"></select>
  5. <button type="button" onclick="dologin()">登录</button>
  6. <button type="button" onclick="dologin1()">登录2</button>
  7. </form>
  8. <script type="text/javascript">
  9. // post (用的最多)
  10. function dologin() {
  11. var _username = $.trim($('input[name="username"]').val());
  12. var _pwd = $.trim($('input[name="pwd"]').val());
  13. // $.post('dologin.php', $('form').serialize(), function(res){
  14. $.post('dologin.php',{username:_username,pwd:_pwd}, function(res){
  15. alert( res.msg );
  16. if(res.code==0){
  17. window.location.href = "home.html";
  18. }
  19. }, 'json');
  20. }
  21. // get
  22. function dologin1() {
  23. var _username = $.trim($('input[name="username"]').val());
  24. var _pwd = $.trim($('input[name="pwd"]').val());
  25. $.get('dologin.php?username='+_username+'&pwd='+_pwd, function(res){
  26. alert( res.msg );
  27. if(res.code==0){
  28. window.location.href = "home.html";
  29. }
  30. }, 'json');
  31. }
  32. // 异步加载数据
  33. function getdata(){
  34. $.get('getcity.php',function(res){
  35. if( res.code==0 ){
  36. var html = '';
  37. $.each( res.data, function(i,v){
  38. html += '<option value="'+v.id+'">'+v.title+'</option>';
  39. });
  40. $('select[name="city"]').html( html ); // $('select[name="city"]').empty().append(html);
  41. }
  42. },'json');
  43. }
  44. getdata();
  45. </script>

2、后台验证 dologin.php

  1. // 方便演示,写了$_REQUEST
  2. $username = $_REQUEST['username']; // $_POST['username'], $_GET['username']
  3. $pwd = $_REQUEST['pwd'];
  4. if($username!='admin'){
  5. $data = json_encode( ['code'=>1,'msg'=>'用户名错误'] );
  6. exit($data);
  7. }
  8. if($pwd!='123'){
  9. $data = json_encode( ['code'=>1,'msg'=>'密码错误'] );
  10. exit($data);
  11. }
  12. $data = json_encode( ['code'=>0,'msg'=>'登录成功'] );
  13. exit($data);

3、获取数据 getcity.php

  1. $citys = [
  2. ['id'=>3,'title'=>'南京'],
  3. ['id'=>6,'title'=>'无锡'],
  4. ['id'=>7,'title'=>'镇江']
  5. ];
  6. $data = json_encode( ['code'=>0, 'msg'=>'success', 'data'=>$citys] );
  7. exit($data);

4、首页 home.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>系统首页</title>
  6. </head>
  7. <body>
  8. 系统首页
  9. </body>
  10. </html>

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:ajax是非常重要, 特别是在前后端交互时
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