开发者

using ajax to create an authentication app but no return is being put

when i click post the post response that i am getting from firefox is: (when i enter k for username)

password username k

Source: username=k&password=

the print received from the php wont display in the #errorshow div

here is my code (html)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type开发者_JAVA百科="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">


$(function() {

 $("#login_form").submit(function() {


 var uname = $("#username").val();
 var upass = $("#password").val();
 $.post("auth.php", { username: uname, password: upass}, function(data) {
 $("#errorshow").html(data);

 });

 return false;


 });

 });



</script>
</head>
<body>
<form id="login_form" method="post">
<p>Username: <input type="text" id="username" /></p>
<p>Password: <input type="password" id="password" /></p>
<p><input type="submit" value="Login" /></p>
</form>
<div id="errorshow"></div>
</body>
</html>

here is the php (auth.php)

<?php
mysql_connect("","","") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

$username = $_POST['username'];
$password = $_POST['password'];



$query = mysql_query("select username,password,active,role from tbl_logins where username = '$username'") or die(mysql_error());
 while($row = mysql_fetch_assoc($query)) {

 // CHECK FOR EMPTY FIELDS
    if(empty($username) || empty($password)) {
    print "the username or password is empty";
    }




    //CHECK FOR USERNAME EXISTANCE
    elseif($db->num_rows($query) == 0) {
 print "the username you entered doesnt exist";
 }


    //CHECK FOR PASSWORD IS CORRECT
    elseif(sha1($password) != $row['password']) {
 print "the password you entered is incorrect";
 }


     //CHECK IF ACCOUNT IS ACTIVE
     elseif($row['active'] == 'no') {
     print "your account is not active. please verify your email";
  }


 else {

 //create cookie and login user.

 $_SESSION['AUTHID'] = sha1($row['hash']);

 //routh to appropriate role section
 if($row['role'] == "0") {
 $_SESSION['error'] = "";
 header("Location: /_admin/dashboard.php");
 } else {
 $_SESSION['error'] = "";
 header("Location: /worldex/_members/index.php");
 }

 }//end else


 } //end while


?>


Try changing the line:

$(function() {

to

$(document).ready(function() {

As your script is trying to bind the 'submit' method to a form that doesn't exist (until the page has loaded)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜