post前端部分(ajax)
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<form>
<p><label for="user">姓名:<input type="text" name="user" id="user"></label></p>
<p><label for="password">密码:<input type="password" name="password" id="password"></label></p>
<button onclick="ajax()" type="button">提交</button> //type='button'才能ajax
</form>
//post提交
<script>
function ajax(){
//serialize() 获取所有能填写的数据的input等
$.post('/demo.php',$('form').serialize(),function(reg){
if(reg.code>0){
console.log(reg.msg);
}else{
console.log(reg.msg);
}
},'json')
}
</script>
post后端部分(ajax)
class users
{
public function show()
{
$user = $_POST['user'];
$password = $_POST['password'];
if($user !== 'admin'){
$date = json_encode(['code'=>1; 'msg'=>'账户不正确']); //转换json数组,才可返回
die($date);
}
if($password !== '123456'){
$date = json_encode(['code'=>1; 'msg'=>'密码错误']);
die($date);
}
$date = json_encode(['code'=>0; 'msg'=>'登陆成功']);
die($date);
}
}
$user = new users;
$user->show();
get前端部分(ajax)
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<form>
<p><label for="user">姓名:<input type="text" name="user" id="user"></label></p>
<p><label for="password">密码:<input type="password" name="password" id="password"></label></p>
<button onclick="ajax()" type="button">提交</button> //type='button'才能ajax
</form>
//post提交
<script>
function ajax(){
//serialize() 获取所有能填写的数据的input等
//$.get('/demo.php?user=user&password=password',function(reg){},'json')
$.get('/demo.php',$('form').serialize(),function(reg){
if(reg.code>0){
console.log(reg.msg);
}else{
console.log(reg.msg);
}
},'json')
}
</script>
get后端部分(ajax)
class users
{
public function show()
{
$user = $_GET['user'];
$password = $_GET['password'];
if($user !== 'admin'){
$date = json_encode(['code'=>1; 'msg'=>'账户不正确']); //转换json数组,才可返回
die($date);
}
if($password !== '123456'){
$date = json_encode(['code'=>1; 'msg'=>'密码错误']);
die($date);
}
$date = json_encode(['code'=>0; 'msg'=>'登陆成功']);
die($date);
}
}
$user = new users;
$user->show();
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!