2012-07-03 44 views
0

我正在尝试为我的jQuery移动应用程序创建一个登录系统,但点击登录按钮后它没有做任何事情。现在我没有使用数据库连接。jQuery Mobile和PHP登录系统不工作

的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); 
} 
?> 

我得到 '连接...' 在#output但仅此而已。

+0

是'postTo'横域请求? – Ohgodwhy

+0

是的。这是一个移动应用程序 – RapsFan1981

+0

我有同样的确切问题!我一直在试图找到一个约5个月的解决方案!任何帮助都会很棒。谢谢。 –

回答

1

您输入的用户名字段为“user”。

更改您的JavaScript看起来像这样

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

那么你应该的login.php looke这样的:

<?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); 
} 
?> 
+0

应该在寻找$ _POST ['userLbl']?{userLbl:$('[name = username]')。val() – Waygood

+0

yes yes yes !!! you are rigth!$ _POST ['userLbl']。Thanks! – nicowernli

+0

感谢大家! :) – RapsFan1981

4

的login.php中的代码对第2行语法错误它应显示为:

if(isset($_POST['userLbl']) && isset($_POST['passwordLbl']))