douhun8647 2012-08-01 08:17 采纳率: 0%
浏览 59
已采纳

使用jQuery AJAX验证用户无法正常工作

I'm trying to authenticate a user using AJAX wrapped with jQuery to call a PHP script that queries a MySQL database. I'm NOT familiar with any of those technologies but I (sorta) managed to get them working individually, but I can't get the jQuery, AJAX and HTML to work properly.

[Edit:] I followed Trinh Hoang Nhu's advice and added a return false; statement to disable the Submit button. All previous errors fixed, I can't get the object returned by the AJAX right.

HTML

Here's the HTML snippet I use:

<form id="form" method='post'>
    <label>Username:</label>
    <input id="user" name="user" type="text" maxlength="30" required /> <br />
    <label>Password:</label>
    <input id="pass" name="pass" type="password" maxlength="30" required /> <br />
    <input id="url" name="url" type="text" value="<?php echo $_GET['url'] ?>" hidden />
    <input name="submit" type="submit" value="I'm done">
  </form>

jQuery/AJAX

Here's my jquery code for using AJAX to authenticate a user (sorry if the indenting is messed up because of the tabs):

function changeMessage(message) {
    document.getElementById("message").innerHTML = message; }

$(document).ready(function() {
    $("#form").submit(function() {
        changeMessage("Checking");
        //check the username exists or not from ajax
        $.post("/stufftothink/php/AJAX/login.php",
                    {user: $("#user").val(), pass: $("#pass").val(), url: $("#url") },
                    function(result) {
                        //if failed
                        if (result === 'false') {
                            changeMessage("Invalid username or password. Check for typos and try again");
                            $("#pass").val(""); }
                        //if authenticated
                        else {
                            changeMessage("Authenticated");
                            window.location.href = result; } 
                    } );
        //to disable the submit button
        return false;
    }   );
} )

PHP

And here's my PHP script that gets called:

<?php 
ob_start();
session_start();

$user = $_POST['user'];
$pass = md5($_POST['pass']);

mysql_connect('localhost', 'root', '');
mysql_select_db('stufftothink');

$query = "select * from users where user = '$user' and pass = '$pass'";
$result = mysql_query($query);

$i = 0;
while ($row = mysql_fetch_array($result)) {
  $i = 1; }

if ($i == 1) {
  $_SESSION['user'] = $user; 
    $invalid_urls = array('register.php', 'login.php');
    $url = $_REQUEST['url']; //not sure whether _GET or _POST
    if (in_array($url, $invalid_urls)) {
        echo '/stufftothink/profile.php'; }
    else {
    echo '/stufftothink/'.$url; }
    }

else {
  echo 'false'; }

mysql_close();
?>

Edit

I've been getting a lot of downvotes on this question. I had accidentally submitted the question without the explanation filled in. I went back to edit it, but when I came back, there were already 4 downvotes. It had barely been a couple of minutes. Am I doing something wrong, or were the first 5 minutes the problem?

  • 写回答

2条回答 默认 最新

  • doujing6436 2012-08-01 14:34
    关注

    First if you want to submit form using ajax, you must return false from your submit function. Otherwise your browser will handle it and redirect you to another page.

    If you want to return an object from PHP, you must convert it to json using json_encode, for example:

    //PHP
    $return = array("url" => "http://www.google.com");
    echo json_encode($return);
    //would echo something like {"url":"http://www.google.com"}
    
    //JS
    $.post(url, data, function(data){
      alert(data.url);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的