使用Jquery mobile、PHP和MySQL的移动应用程序登录表单

使用Jquery mobile、PHP和MySQL的移动应用程序登录表单,php,jquery-mobile,mysqli,Php,Jquery Mobile,Mysqli,我是新的移动发展。我正在尝试开发一个登录表单,它将检查MySQL数据库的用户名和密码。我已经构建了一些代码。我有两个问题。第一个是,在提交表单后,在浏览器的URL栏中,我可以看到输入的用户名和密码,甚至可以在我的JQuery脚本上使用POST。第二个问题是页面一直返回login.html,而不是main.html。下面是我使用PHP、JQuery和MySQL的实际经验构建的内容 HTML表单 <div id="loginPage" data-role="page"> <div

我是新的移动发展。我正在尝试开发一个登录表单,它将检查MySQL数据库的用户名和密码。我已经构建了一些代码。我有两个问题。第一个是,在提交表单后,在浏览器的URL栏中,我可以看到输入的用户名和密码,甚至可以在我的JQuery脚本上使用POST。第二个问题是页面一直返回login.html,而不是main.html。下面是我使用PHP、JQuery和MySQL的实际经验构建的内容

HTML表单

<div id="loginPage" data-role="page">
<div data-role="header">
<h1>App Authentication</h1>
</div>

<div data-role="content">   
    <div id="landmark-1" data-landmark-id="1">
    <form id="loginForm">
    <div data-role="fieldcontain" class="ui-hide-label">
        <label for="username">Username:</label>
        <input type="text" name="username" id="username" value="" placeholder="Username" />
    </div>

    <div data-role="fieldcontain" class="ui-hide-label">
        <label for="password">Password:</label>
        <input type="password" name="password" id="password" value="" placeholder="Password" />
    </div>

    <input type="submit" value="Login" id="submitButton">
    </form>
</div>  
</div>

<div data-role="footer">
    <h4>&copy; Silva's General Services</h4>
</div>

应用程序身份验证
用户名:
密码:
&抄袭;席尔瓦总务部

auth.php

<?php
$mysqli = new mysqli($mysql_hostname,$mysql_user, $mysql_password, $mysql_database);

header("access-control-allow-origin: *");
header("access-control-allow-methods: GET, POST, OPTIONS");
header("access-control-allow-credentials: true");
header("access-control-allow-headers: Content-Type, *");
header("Content-type: application/json");

// Parse the log in form if the user has filled it out and pressed "Log In"
if (isset($_POST["username"]) && isset($_POST["password"])) {

      CRYPT_BLOWFISH or die ('No Blowfish found.');

      //This string tells crypt to use blowfish for 5 rounds.
      $Blowfish_Pre = '$2a$05$';
      $Blowfish_End = '$';

      $username = mysql_real_escape_string($_POST['username']);
      $password = mysql_real_escape_string($_POST['password']);


      $sql = "SELECT id, password, salt, username FROM users WHERE username='$username' LIMIT 1";
      $result = $mysqli->query($sql) or die( $mysqli->error() );
      $row = mysqli_fetch_assoc($result);

      $hashed_pass = crypt($password, $Blowfish_Pre . $row['salt'] . $Blowfish_End);

      if ($hashed_pass == $row['password']) {
          $response_array['status'] = 'success'; 
      } else {
          $response_array['status'] = 'error'; 
      }
echo json_encode($response_array);

}
$mysqli->close();
?>

这只是一个猜测,但是:

  • 表单声明中没有方法:

  • 看起来您没有在函数中传递事件。我知道语法是这样的:
    $('#loginForm').submit(函数(e){
    注意函数参数为“e”

  • jquery有
    e.preventDefault
    e.stopPropagation
    ,这是两个独立的东西,请参阅。其他一些处理程序可能正在处理这些事情。您也可以使用“return false;”而不是这两个,具体取决于您的用例


  • 我认为2.要解决您的问题,您需要将事件传递给函数。

    Hi@Bogdan!太棒了。小“e”正在完成任务。非常感谢您的回答!
    <script>
    $('#loginForm').submit(function(){
     e.preventDefault();
     jQuery.support.cors = true; 
     $.ajax({ 
         url: 'http://www.test.com/mobile/auth.php',
         crossDomain: true,
         type: 'post',
         data: $("#loginForm").serialize(), 
         success: function(data){
             if(data.status == 'success'){
                 window.location.href = 'http://www.test.com/mobile/main.html'; 
             }else if(data.status == 'error'){
                 alert("Authentication Invalid. Please try again!");
                 return false;        
            }
    
         }
     }); 
    
     }); 
    </script>