-5

My code works when I don't use the username and password in the db. But if I do match the details in the db its a problem. But if I match those in the in a script it works. Whats wrong? I am expecting the code to show the success message without refreshing and after it redirectime to the view.php : it works the way it is but if i integrate the db matching the php then jquery will keep on telling me wrong user name or password yet the password and user name are fine

<script type="text/javascript">
$(document).ready(function() {

$("#login").click(function() {

    var action = $("#form1").attr('action');
    var form_data = {
        username: $("#username").val(),
        password: $("#password").val(),
        is_ajax: 1
    };

    $.ajax({
        type: "POST",
        url: action,
        data: form_data,
        success: function(response)
        {
            if(response == 'success')
                $("#form1").slideUp('slow', function() {
                //function to direct 

window.location.replace("view.php");
                //$("#message").html("<p class='success'>You          logged in successfully!</p>");
                });
            else
                $("#message").html("<p class='error'  style='color:red'>Invalid username and/or password.</p>");    
        }
    });

    return false;
});
// Function that redirects the user to another page
});
</script>

PHP file if it is like this it works great.

<?php

    $is_ajax = $_REQUEST['is_ajax'];
    if(isset($is_ajax) && $is_ajax)
{
    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];

    if($username == 'demo' && $password == 'demo')
    {
        echo "success"; 
    }
}

?>

The php code which matches with the db

<?php

$is_ajax = $_REQUEST['is_ajax'];
if(isset($is_ajax) && $is_ajax)
{
$username =(isset($_POST['username']))? trim($_POST['username']): '';
$password=(isset($_POST['password']))? $_POST['password'] : '';

// 
$query ='SELECT username FROM site_user WHERE '.
'username="'.mysql_real_escape_string($username,$con).'" AND ' .
'password = md5("'.mysql_real_escape_string($password,$con).'")';
$result=mysql_query($query,$con) or die (mysql_error($con));
if(mysql_num_rows($result)>0){
echo 'ok';
}

}
?>  
DMK
  • 2,448
  • 1
  • 24
  • 35
Grader
  • 11
  • 1
  • 3
  • 5
    what does jQuery have to do with your database? – Kevin B Jan 16 '13 at 15:51
  • 1
    jQuery is running client-side. Your database is running server-side. – Sparky Jan 16 '13 at 15:52
  • Have you used something like Firebug to see what response your AJAX request returns? Or in the ajax call use: `error: function(e){ alert(e.status); }` If you're getting a 500 error then there's an issue with your action URL page – ajtrichards Jan 16 '13 at 15:53
  • Why does this have a php tag? – Jeremy A. West Jan 16 '13 at 15:54
  • 1
    You need to explain what you expect this script to do, and what it's doing instead. Then we can help you figure out why there's a difference. If you don't understand what the code is supposed to do, that's your question. Right now it's not clear what your question is. – pjmorse Jan 16 '13 at 15:54
  • What do you mean by "if it do use the db matching"? That isn't a phrase I understand. – pjmorse Jan 16 '13 at 15:55
  • Typing in all caps isn't going to do more than get you closed and down-voted faster. – Sparky Jan 16 '13 at 15:55
  • There's a few useful and relevant links in the *Related* section on the right hand side. This is one of them: http://stackoverflow.com/questions/363528/jquery-form-processing-with-php-to-mysql-database-using-ajax-request Study it, hopefully it will help you to understand your own code better. – maksimov Jan 16 '13 at 16:00
  • Please DO NOT use all CAPS when posting questions and responses. It is not helpful and "yelling" doesn't make things clearer. Be respectful and you will be respected. – gtr1971 Jan 16 '13 at 16:04
  • Sorry I did not know that I wont do that again – Grader Jan 16 '13 at 16:17

2 Answers2

1

The url: parameter isn't getting a URL, first of all... but there are other issues.

Use this as an example (borrowed from another question/answer here... jQuery Ajax POST example with PHP)

/* get some values from elements on the page: */
var values = $(this).serialize();

/* Send the data using post and put the results in a div */
$.ajax({
  url: "test.php",
  type: "post",
  data: values,
  success: function(){
      alert("success");
       $("#result").html('submitted successfully');
  },
  error:function(){
      alert("failure");
      $("#result").html('there is error while submit');
  }   
});
Community
  • 1
  • 1
gtr1971
  • 2,672
  • 2
  • 18
  • 23
0

Pass correct values to ajax function

$.ajax({
        type: "POST",
        url: your php file handling form data,
        data: serialize the form data,
        success: function(response){}

});
rizon
  • 8,127
  • 1
  • 27
  • 17