dongyong3554 2012-07-03 14:59
浏览 55
已采纳

jQuery Mobile和PHP登录系统无法正常工作

I'm trying to create a login system for my jQuery mobile app but it's not doing anything after the login button is clicked. Right now I'm not using a db connection.

index.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="jquery.mobile-1.1.0/jquery.mobile.theme-1.0.min.css" rel="stylesheet" type="text/css">
<link href="jquery.mobile-1.1.0/jquery.mobile.structure-1.1.0.min.css" rel="stylesheet" type="text/css">
<script src="jquery.mobile-1.1.0/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
    $('#loginform').submit(function(){
    $('#output').html('Connecting...');
    var postTo = 'http://www.hedonsoft.com/game/login.php';
    $.post(postTo,{userLbl: $('[name=username]').val(), passwordLbl: $('[name=password]').val()}, function (data){
        if(data.message){
            $('#output').html(data.message);    
        }else{
            $('#output').html("Could not connect"); 
        }
        }, 'json');

        return false;
    });
    }); 
</script>
<script src="jquery.mobile-1.1.0/jquery.mobile-1.1.0.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="custom.css" />
</head>

<body>
<div data-role="page" id="mc_main">
  <div data-role="header" id="header">
    <h1>Main Menu</h1>
  </div>

<div data-role="content">
<div data-role="collapsible" id="logindiv"><h2>Login</h2>
   <form id="loginform" method="post">
 <div data-role="fieldcontain">
      <label for="user">Username:</label>
      <input type="text" name="user" id="user" value=""  />
    </div>
    <div data-role="fieldcontain">
      <label for="pass">Password:</label>
      <input name="pass" type="password" id="pass" value="">
    </div>
    <input type="submit" value="Login" data-icon="check" data-theme="a"/>
   </form>
   <div id="output"></div>
       </div>
</div>
</body>
</html>

login.php

<?php
if(isset($_POST['username'] and isset($_POST['password'])) {

 // do logic for logining in (usually query your db)
 if ($_POST['username'] == 'test' and $_POST['password'] == 'test') {
 $data['success'] = true;
 $data['message'] = 'Login succesful';
 } else {
 $data['success'] = false;
 $data['message'] = 'Login failed';
 }
 // return json
 echo json_encode($data);
}
?>

I get 'Connecting...' in #output but that's it.

  • 写回答

3条回答 默认 最新

  • drbfxb977777 2012-07-03 15:07
    关注

    Your input name for the user field is "user".

    Change your javascript to look like this

    $.post(postTo,{userLbl:$('[name=user]').val(), ...
    

    Then your login.php should looke like this:

    <?php
    if(isset($_POST['userLbl'] and isset($_POST['passwordLbl'])) {
    
     // do logic for logining in (usually query your db)
     if ($_POST['userLbl'] == 'test' and $_POST['passwordLbl'] == 'test') {
     $data['success'] = true;
     $data['message'] = 'Login succesful';
     } else {
     $data['success'] = false;
     $data['message'] = 'Login failed';
     }
     // return json
     echo json_encode($data);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?