0

I have a login form using jquery ajax and a php code. The issue is that is always returns an error instead of logging me into the system. I have spent days trying to find the error but I can't. The webpage displays no errors if I visit it directly. This used to work about a week ago until I accidentally deleted the jquery and now I can't get it to work.

Here is the PHP code:

include('.conf.php');
$user = $_POST['user'];
$pass = $_POST['pass'];
$result = mysql_query("SELECT * FROM accountController WHERE username = '$user'");
while ($row = mysql_fetch_array($result)) {
    if (sha1($user.$pass) == $row['pword']) {
        setcookie('temp', $row['username']);
        session_start();
        $_SESSION['login'] = 1;
        $_SESSION['uname'] = $row['username'];
        echo "success";
    }
} 

and here is the Jquery AJAX code:

var username = $('#main_username').val();
var password = $('#main_pword').val();
    $('.mainlogin').submit(function() {
        $.ajax({
            url: 'log.php',
            type: 'POST',
            data: {
              user: username,
              pass: password
            },
            success: function(response) {
                if(response == 'success') {
                    window.location.reload();
                } else {
                    $('.logerror').fadeIn(250);
                }
            }
        });
        return false;
    });

How would I check to see what is being returned like blank or success from the server. Is there any extension for safari? Thanks!

2 Answers 2

2

Put this in, instead. It will alert the server's response in a popup.

 success: function(response) {
     alert(response);
 }

On your PHP side, try outputting more stuff - add an }else{ to your if statement that returns some useful data, for example

 }else{
    print("Post is: \n");
    print_r($_POST);
    print("\nMySQL gave me: \n");
    print_r($row); 
 }

Just make sure you don't leave it in once it's working!

Look for:

  • hash is different
  • db field is different
  • db fields are blank for some reason
  • POST data is wrong / wrong field names

Edit: Here's another issue: Your first four lines should actually be:

    $('.mainlogin').submit(function() {
        var username = $('#main_username').val();
        var password = $('#main_pword').val();
        $.ajax({

Your code as it is gets the values before the form is submitted - at load - when they are blank, and as a result is sending blank strings to the server as username and password.

Sign up to request clarification or add additional context in comments.

7 Comments

Ok thanks for responding so fast. I changed it and added the alert but it only shows a blank modal window. new ideas?
Also, change your php so that your existing if statement has an }else{ print("error"); } so you can tell if the response is going through or not
Ok now the alert just says error. Can I make it more specific? I am kinda new to PHP.
You could make it so that it returns print_r($_POST), which will print an array of every POST value it is receiving. This will tell you that your form isn't sending the right values (as opposed to your php interpreting them wrong)
Did you change your AJAX code at the top so that $('.mainlogin').submit(function(){ comes before var username = ... ?
|
0

Take a look at the code i have in this answer here: user check availability with jQuery

You need to json encode your response, also Firefox has Firebug you can use to view your ajax post and response, chrome and IE both have developer tools

Safari also has dev tools: http://developer.apple.com/technologies/safari/developer-tools.html

Check the console to see what is happening with your AJAX

2 Comments

This isn't required and actually wouldn't solve his issue. He's just looking for a way to see what values are being returned - trying to add JSON to a currently broken program would only complicate the issue.
Whether its required or not my code sample will work and the bigger issue is he should be using some dev tools

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.