1
function do_login()
{
$('.loginsubmit').click(function(e)
{
    var datum=$('.loginform').serialize();
    $.ajax({
        type: "POST",
        url: "login",
        data: datum,
        contentType: "application/json; charset=utf-8",
        dataType:"json",
        success: function(data)
        {
            if(data.prop=="false")
            {
                $('p#login-error-notif').text(data.error);
            }
            else if(data.prop=="true")
            {
                $('#logindrp span').text("Welcome "+data.name);
            }
        }
    });            
});
} //closing do_login function

This is the function called when user click on the Login the button. The error I see is the Uncaught reference error: 'prop' of null.

The code in the PHP page is

        if(isset($_POST['username']))
        {
         $username=$_POST['username']

...// proceed with checking the username and password

    if($check_login)
    {
    $return=array(prop=>"true", name=>$name, username=>$username);  
        echo json_encode($return);  
    }
            else
            {
            $return=array(prop=>"false", error=>"Invalid username/password.");  
        echo json_encode($return);
            }


        }

But I observed this part is not being executed. I mean the 'username' is not sent to the page. I checked it with adding the code setcookie("username", $username); inside it. The cookie is never created. What is the problem in my AJAX request?

9
  • Obiviously username is not in datum. But what data is? Check with sth like $.each(datum, function(key, value) { alert(key +' => '+ value); }); Commented Aug 8, 2013 at 12:27
  • have you used var_dump the post and then use the ajax to out put the data on screen to check what values have been submitted? Commented Aug 8, 2013 at 12:29
  • @AmazingDreams: datum is serialized with username and password. Also I passed username and password individually. But no use. Commented Aug 8, 2013 at 12:30
  • Fix data: datum and add php last json echo to question. Commented Aug 8, 2013 at 12:30
  • What do you get when you log data ? Commented Aug 8, 2013 at 12:31

2 Answers 2

1

The contentType property in your AJAX call sets the format of the data sent to the server. By default PHP won't accept JSON. Remove the contentType line from your AJAX call.

By default JQuery sends the data as application/x-www-form-urlencoded; charset=UTF-8, which is likely what your PHP script likely needs to fill the $_POST array properly.

This explains why your username value is not getting set. This is also a very common mistake that people make all of the time.

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

4 Comments

Thank you @marteljn, on removing the line I can see the cookie is getting set. But I am not getting the response back from JSON. Still I am getting this error Uncaught TypeError: Cannot read property 'prop' of null
@SuryaS I think you are trying to access it the wrong way. Do a success: function(data){console.log(data) and post the output.
@SuryaS it looks like maybe those if blocks are not getting hit meaning that no output is getting outputted as JSON.
I have found the error. Seems the prop and error must be enclosed within quotes :) Thank you @marteljn
0

It worked well after resolving the following errors:

  1. contentType: "application/json; charset=utf-8", Thank you marteljn.

  2. Enclosing the variables prop, error, username in single codes.

Comments

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.