weixin_33698823 2013-08-21 03:29 采纳率: 0%
浏览 11

无法获得Ajax发布信息

I am able to the js file to fire which does do the first alert but i cannot get the 2nd alert to happen, php file is there and working returning 0 but the alert('finished post'); is not coming up. I think its some syntax I am missing.

$(function () {
$("#login_form").submit(function () {
    alert('started js');
    //get the username and password  
    var username = $('#username').val();
    var password = $('#password').val();

    //use ajax to run the check  
    $.post("../php/checklogin.php", { username: username, password: password }, 
        function (result) {
            alert('finished post');
            //if the result is not 1  
            if (result == 0) {
                //Alert username and password are wrong 
                $('#login').html('Credentials wrong');
                alert('got 0');
            }
    });
});

});

Here is the php

session_start();
include 'anonconnect.php';

// username and password sent from form 
$myusername= $_POST['username']; 
$mypassword= $_POST['password']; 

$sql = $dbh->prepare("SELECT * FROM Users WHERE UserLogin= :login");
$sql->execute(array(':login' => $myusername));
$sql = $sql->fetch();

$admin = $sql['admin'];

$password_hash = $sql['UserPass'];
$salt = $sql['salt'];

/*** close the database connection ***/
$dbh = null;

if(crypt($mypassword, $salt) == $password_hash){
    // Register $myusername, $mypassword and redirect to file
    $_SESSION['myusername'] = $myusername;
    $_SESSION['loggedin'];
    $_SESSION['loggedin'] = 1;

    if($admin == 1){
        $_SESSION['admin'] = 1;
    }

    header("location:search.php");
}
else {
    $_SESSION['loggedin'];
    $_SESSION['loggedin'] = 0;
    echo 0;
}
  • 写回答

4条回答 默认 最新

  • weixin_33695082 2013-08-21 03:44
    关注

    Ah, so damned obvious. You aren't cancelling the default submit action so the form is submitting normally. Add this

    $("#login_form").submit(function (e) {
        e.preventDefault();
    
        // and so on
    

    See http://api.jquery.com/event.preventDefault/

    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试