Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am currently working on a php project. I need to post two values, a username and password value to a php script to check that a user account exists.

I have it working fine posting the username only but don't know how to post multiple values to it.

Below is the code that I currently have

function checkAccount()
        {
            var username = $(\'#txtUsername\').val();
            var password = $(\'#txtPassword\').val();

            $.post("phpHandler/login.php"), {username: username},
                function(result)
                {
                    if (result == "unknownUser")
                    {
                        $(\'#msg\').html("Unknown username and password").addClass(\'formError\');
                        $(\'#msg\').fadeIn("slow");
                    }
                }
        }

For the line

$.post("phpHandler/login.php"), {username: username},

I tried to do

$.post("phpHandler/login.php"), {username: username, password: password}, but this doesn't seem to work.

Thanks for any help you can provide

share|improve this question
2  
Why are you escaping the quotes in your $() calls? That turns the quotes into "part-of-the-string" so jquery is looking for some element literally named '#txtPassword', and not an element with id #txtPassword. –  Marc B Oct 27 '11 at 21:57
    
its because its within a php script and the html/javascript is surrounded with ' that's why its escaped so its not actually looking for '#txtPassword' –  Boardy Oct 27 '11 at 22:05
add comment

2 Answers

up vote 3 down vote accepted

You have syntax errors all over the place between your escaped quotes and the early closing parens on your .post call. This code will work assuming no other issues exist in your supporting code:

function checkAccount()
{
    $.post(
        'phpHandler/login.php',
        {
            username: $( '#txtUsername' ).val(),
            password: $( '#txtPassword' ).val()
        },
        function( result )
        {
            if( result === 'unknownUser' )
            {
                $( '#msg' )
                    .html( 'Unknown username and password' )
                    .addClass( 'formError' )
                    .fadeIn("slow");
            }
        }
    );
}
share|improve this answer
add comment

Try something like:

$.post("phpHandler/login.php", {username: username, password: password}, function(){
  // success code goes here
});

You are closing the parenthesis of post function before passing the parameters.

share|improve this answer
    
This doesn't seem to work either, i'm not sure that it`s calling the underneath function where it checks the result of the post statement –  Boardy Oct 27 '11 at 22:02
1  
@Boardy - When using the .ajax shortcuts (such as .post), if anything goes wrong it fails silently. Your success function never runs and no error occurs. If this is a possibility, you should switch to .ajax while developing and provide an error method which lets you know something went wrong. –  JAAulde Oct 27 '11 at 22:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.