ajax用户连接

时间:2012-11-19 22:23:28

标签: ajax cordova

我正在尝试在Ajax中获取userconnexion(将在Phonegap应用程序中使用)。

我没有javascript错误,但我的ajax函数始终处于错误状态,没有详细信息。

这是我的表格:

<form id="loginForm">
                <div data-role="fieldcontain" class="ui-hide-label">
                    <label for="username">Username:</label>
                    <input type="text" name="username" id="user"/>
                </div>
                <div data-role="fieldcontain" class="ui-hide-label">
                    <label for="password">Password:</label>
                    <input type="password" name="password" id="pass"/>
                    </div>
                <input type="text" size="25" id="message" value="error" />
                <input type="button" value="Submit" id="sendButton"/>
                    </form>

这是我的Js文件

$(document).ready(function(){
    $(window).load(function(){
        $('#sendButton').click(function() {
             var user = $("#user").val();
             var pass = $("#pass").val();
             $.ajax({
                 url: "http://myurl.php",
                 type: "GET",
                 data: { username: user, password: pass },
                 success: function (html) {alert(html); },
                 error: function(){alert('false');}
              });
         });

    });
});

这是我的php文件

<?php
$user = $_GET['username'];
$pass = $_GET['password'];

$link = mysql_connect('dbhost', 'dblogin', 'dbpss');
if (!$link) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('dbname',$link); 

$result = mysql_query("SELECT email, mdp, id FROM auteur WHERE email = '$user'");
while($row = mysql_fetch_array($result)){
  if(md5($pass) == $row["mdp"]){
     echo $row["id"];
  } else {
     echo '';
  }
} 
?>

关于我的PHP我尝试http://myurl.php?password=test&username=test我得到了我的ID,所以我觉得这部分有效。

但是当我尝试使用相同值的表单时,我得到一个'false',所以出现错误......

我是新手,有人可以解释我的错误吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

<script>
    function submitme(){
         var user = $("#user").val();
         var pass = $("#pass").val();
         $.ajax({
             url: "http://myurl.php",
             type: "GET",
             data: { username: user, password: pass },

          }).done(function(answer){
             alert(answer + 'server is saying okay');
       });
    }
</script>

html:

    <form id="loginForm">
            <div data-role="fieldcontain" class="ui-hide-label">
                <label for="username">Username:</label>
                <input type="text" name="username" id="user"/>
            </div>
            <div data-role="fieldcontain" class="ui-hide-label">
                <label for="password">Password:</label>
                <input type="password" name="password" id="pass"/>
                </div>
            <input type="text" size="25" id="message" value="error" />
            <input type="button" value="Submit" id="sendButton" onclick="submitme()"/>
    </form>

试试这段代码。