简体   繁体   中英

Post multiple values via javascript ajax to php

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM