Post multiple values via javascript ajax to php - Stack Overflow

I am currently working on a php project. I need to post two values, a username and password value to a

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

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 asked Oct 27, 2011 at 21:52 BoardyBoardy 36.3k108 gold badges271 silver badges463 bronze badges 2
  • 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 Commented Oct 27, 2011 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 Commented Oct 27, 2011 at 22:05
Add a ment  | 

2 Answers 2

Reset to default 3

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");
            }
        }
    );
}

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745225000a4617419.html

相关推荐

  • Post multiple values via javascript ajax to php - Stack Overflow

    I am currently working on a php project. I need to post two values, a username and password value to a

    5小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信