Javaaaa
Javaaaa

Reputation: 3804

How to send two inputs with jQuery

ok, i have these two input fields where a user puts in two twitter names. When the submit button is pressed, both names should be send to a .php file with the POST method that checks if both usernames exsist on twitter.

Sending and receiving the answer for one value already works, but how can i also add the second? I already have this:

<script type="text/javascript">
function checkUsername()
{

    $.ajax({
        type: 'POST',
        url: 'tu.php',
        **data: {'user1' : $('#user1').val() },** //how to append user2?
        dataType: "json",
        success: function(data){
            $('#uitslag').html(JSON.stringify(data)); 
            $('#user1text').html(data['user1']);
            $('#user2text').html(data['user2']);
        }
    });
}
</script>

the fields in the form:

    <td><input type="text" name="user1" id="user1"/></td>
    <td><input type="text" name="user2" id="user2" /></td>

and this is how the values should be able to be cathed in the .php:

$user1 = $_POST['user1'];
$user2 = $_POST['user2'];

So the question really is: how can I append the second username to the above jQuery POST function?

p.s. I am starting with javascript and jQuery, how do you guys work with this as no error messages are shown ever.. is there an environment/programm where I get debugging help with javascript?

Upvotes: 0

Views: 151

Answers (4)

jnoreiga
jnoreiga

Reputation: 2175

You actually don't even need the serialize function. If you just select your form, all form elements will be passed. This way if you just add another form element, like another textbox, it will all be passed in your ajax call.

function checkUsername()
{

    $.ajax({
        type: 'POST',
        url: 'tu.php',
        data: $("#your_form"),
        dataType: "json",
        success: function(data){
            $('#uitslag').html(JSON.stringify(data)); 
            $('#user1text').html(data['user1']);
            $('#user2text').html(data['user2']);
        }
    });
}

Upvotes: 1

Andy Gaskell
Andy Gaskell

Reputation: 31761

You're probably looking for serialize. Your code would look something like this:

function checkUsername()
{

    $.ajax({
        type: 'POST',
        url: 'tu.php',
        data: $("#your_form").serialize(),
        dataType: "json",
        success: function(data){
            $('#uitslag').html(JSON.stringify(data)); 
            $('#user1text').html(data['user1']);
            $('#user2text').html(data['user2']);
        }
    });
}

If you're sure you don't want serialize you could try this:

data: {'user1' : $('#user1').val(), 'user2' : $('#user2').val() }

As for your PS, check out Firebug and Webkit developer tools.

Upvotes: 1

Cfreak
Cfreak

Reputation: 19309

data: {
     'user1' : $('#user1').val(), 
     'user2' : $('#user2').val()
},

Upvotes: 2

Platinum Azure
Platinum Azure

Reputation: 46183

It's a simple enough extension-- just follow the same pattern.

<script type="text/javascript">
function checkUsername()
{

    $.ajax({
        type: 'POST',
        url: 'tu.php',
        data: {
            'user1' : $('#user1').val(),
            'user2' : $('#user2').val()
        },
        dataType: "json",
        success: function(data){
            $('#uitslag').html(JSON.stringify(data)); 
            $('#user1text').html(data['user1']);
            $('#user2text').html(data['user2']);
        }
    });
}
</script>

That said, jQuery does also have a .serialize() function that you could apply on the containing form, which automatically serializes the whole form. This could prove useful for you.


EDIT: It's worth mentioning that the jQuery selectors above look on the id for the name "user1" (etc.), whereas the PHP script expects the form elements' name to be "user1" (etc.). Here you have them as the same thing.

A more reliable jQuery selector that would allow you to always use the name in both jQuery and PHP is simply to use an attribute selector in jQuery:

$('input[name="user1"]').val()

This will catch any <input> element with the name attribute set to "user1".

Upvotes: 1

Related Questions