最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Post multiple values via javascript ajax to php - Stack Overflow

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论