dougao9864 2017-10-15 14:04
浏览 31

使用AJAX,php,jQuery创建一个简单的登录表单

I have a login form like this: I have 3 files: index.html, login.php, custom.js

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Login</title>  
  </head>
  <body>    
    <section>
      <div id="message"></div>    
      <form id="loginform" method="POST">
        <p>Username: <input type="text" id="username" name="username" size=25  />
        </p>
        <p>Password: <input type="password" id="password" name="password" size=25 />
        </p>
        <button id="login" type="submit" name="submit">Login</button>
      </form>    
    </section>   
  </body>
</html>

custom.js looks this way:

$('#login').click ( function() {
  $.ajax({
    type: "POST",
    url: 'login.php',
    data: $('#loginform').serialize(),
    dataType: 'json',
    success: function(data) {
      //alert(data.message);
      // print into ( <div id="message"></div>) on file html.               
    },
  });
  return false;
});

login.php looks this way:

$users = array(
  0 => array(
    "username" => "admin",
    "password" => "admin",
  ),
  1 => array(
    "username" => "demo",
    "password" => "demo",
  ),
);

Please check my code below:

for ( $i=0; $i< count($users) ; $i++ ){
  if ( isset($_POST['username'])&& isset($_POST['password']) ) {
    // Please check this code
    if ( $_POST['username'] == $users[$i]['username'] && $_POST['password'] == $users[$i]['password'] ) {
      $data = array('result' => 'success','message' => 'Message sent from server');
      echo json_encode($data);
      // Please check this code
    } else {
      // Please check this code
      $data = array('result' => 'fail', 'message' => 'Message not sent from server');
      echo json_encode($data);
    }
  }
}

I want to check user (array) with users from Form login, use AJAX print message if match user or not.

Please help me check all my code and fix it. Thanks all.

  • 写回答

3条回答 默认 最新

  • duanluanhui8348 2017-10-15 14:20
    关注

    I am not sure if I understand you, i mean the part with i+.... but...can t you just query the table like: query roucount where user = @_post[user] AND password = @_post[pass], then if mysqli rowcount ==1 login, else die+error?

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b