<!DOCTYPE html> <html> <head> <title>登录</title> <meta charset="utf-8"> <script type="text/javascript" src="jquery-3.3.1.min.js"></script> <style type="text/css"> .content{ width: 350px;height: 300px; background-color: #ccc; text-align: center; margin: 100px auto; position: relative; } .content1{ /*margin-top: 50px;*/ width: 100%;height: 200px; background-color: lightgreen; position: absolute;top: 50px; } </style> </head> <body> <div class="content"> <div class="content1"> <h3>用户登录</h3> <p> <label>用户:</label> <input type="username" name="username" id="username"> </p> <p> <label>密码:</label> <input type="password" name="password" id="password" > </p> <p> <button onclick="dologin()">登录</button> </p> </div> </div> </body> </html> <script type="text/javascript" href=""> function dologin(){ // alert(11); //获取qq输入的内容; var user = $('input[name="username"]').val(); var pwd = $('input[name="password"]').val(); if(user == ''){ alert('请输入用户名'); return; } if(pwd == ''){ alert('请输入密码'); return; } //通过post将数据提交到后台进行验证; $.post('ceshi3.php',{qq:user,pwd:pwd},function(data){ // console.log(data); if(data.code==1){ alert(data.msg); }else{ alert(data.msg); // setTimeout(function(){window.location.href='index.php';},2000); } },'json') } </script> ?>
<?php //接收信息 $qq = $_POST['qq']; $pwd = $_POST['pwd']; $data = [ ["username"=>'admin',"password"=>123456], ["username"=>'luheng',"password"=>111111], ["username"=>'zhangsan',"password"=>121212], ]; $like = [] ; foreach($data as $k => $val){ if(in_array($qq, $val)){ $like = $val; } } //$like是一个数组 //判断$like是否有值; if(!$like){ echo json_encode(['code'=>1,'msg'=>'用户名错误']); return; } //判断密码是否存在; if($like['password'] != $pwd ){ echo json_encode(['code'=>1,'msg'=>'密码错误']); return; } exit(json_encode(array('code'=>0,'msg'=>'登录成功'))); ?>
foreach循环二维数组;输出为多个一维数组;
in_array可以通过值找到一维数组;