Hey, I got a little problem with the jQuery AJAX thing, the function(data) part doesn't seem to get called I don't have idea why I've been trying to solve this, but with not luck..

Here is my checklogin function

function checkLogin()
{
	var username = $('#name').attr('value');
	var password = $('#password').attr('value');
	alert(username);
	
	$.ajax({
                        type: "POST",
                        url: "check.php",
                        data: "username=" + username + "& password=" + password,
                        success: function(){
                            alert('Success');
                        },
                        error: function(){
                            alert('failure');
                        }
                    }, function(data){
                        $('error').html(data);
                  });
	

	
}

The error DIV where error should go

<div id="error" class="error" style="width:25%; height:40px; margin-left:39%; margin-top:1%; margin-right:auto; font-size:140%; background:#F33;
		height:30px;
		font-weight:bold;
		color:#000;
		background-color:#F30;">
		</div>

My check.php file

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="asdasdsad"; // Mysql password
$db_name="asdad"; // Database name
$tbl_name="asasdasdd232"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

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

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
	
	echo "Incorrect login!";

}
?>

The incorrect login should appear in error div, but it won't, the function(data) won't get called somehow.

Recommended Answers

All 2 Replies

I think you just left off the dot defining your class.

$('.error').html(data);

According to the docs, ajax() has but one parameter. Yet, you are passing two.

$.ajax({
  type: "POST",
  url: "check.php",
  data: "username=" + username + "&password=" + password,
  success: function(data) {
    $('#error').html(data);
  },
  error: function() {
    alert('failure');
  }
});
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.