5

Okay so here's the idea:

I want to make a jQuery script to handle multiple forms. Every form has a button. When you click this button the script should send all input fields' name and value in $_POST to the form's action URL. But I can't make it work.

It won't loop through my .each on line 3 in my jQuery file.

jQuery:

$("input[type='button']").click(function(){
    var query = {};
    $(this).parent('form').children('input[type="text"]').each(function(){
        query[$(this).attr('name')] = $(this).val();
    });

    $.post($(this).parent('form').attr('action'), query, function(data) {
        $("#dialog").html(data);
        $("#ui-id-1").html('Activate account');
        $("#dialog").dialog("open");
    }, 'json');
});

HTML:

<form action="/action/activateacc.php">
<table>
    <tr>
        <td>E-mail:</td>
        <td><input type="text" id="mail" name="mail" /></td>
        <td><img class="tick" id="mailIMG" width="20px" /></td>
    </tr>
    <tr>
        <td>Activation code:</td>
        <td><input type="text" id="code" name="code" /></td>
        <td><img class="tick" id="codeIMG" width="20px" /></td>
    </tr>
    <tr>
        <td>Choose organization-name:</td>
        <td><input type="text" id="name" name="name" /></td>
        <td><img class="tick" id="nameIMG" width="20px" /></td>
    </tr>
    <tr>
        <td></td>
        <td style="text-align:center"><input type="button" style="cursor:pointer" value="Activate account" /></td>
    </tr>
 </table>
 </form>
0

6 Answers 6

2

Just use .serialize() with the form to get name/values of your inputs- also you should be using closest since parent only goes up one level

$("input[type='button']").click(function(){   
    var query = $(this).closest('form').serialize();
    $.post($(this).parent('form').attr('action'), query , function(data) {
        $("#dialog").html(data);
        $("#ui-id-1").html('Activate account');
        $("#dialog").dialog("open");
    }, 'json');
});
Sign up to request clarification or add additional context in comments.

Comments

0

.parent() only goes up a single level of the DOM - try using .closest() and find() - so

$(this).closets('form').find('input[type="text"]').each(function(){

Comments

0

Use .find to find children and .parents to find the form

$(this).parents('form').find('input[type="text"]').each(function(){
        query[$(this).attr('name')] = $(this).val();
    });

Comments

0

Easiest way for me to read it, would be to use .closest() (which goes up all the way instead of parent which is one level) and also find() (which runs a query through your current jQuery object):

$(this).closest('form').find('input[type="text"]').each(function(){ 
    query[$(this).attr('name')] = $(this).val();
});

jsFiddle Demo

Comments

0

parent() only goes up 1 level in the DOM tree and children() only goes down 1 level in the DOM tree.

You should try either to use more of them if you are 100% sure the structure won't change (not recommended, it's not that clean), or use closest(), that will retrieve the closest given tag name in the DOM tree.

Comments

0

You can spare yourself from the headaches of serializing your form elements into a query by simply using the built-in $.serialize.

The .serialize() method creates a text string in standard URL-encoded notation. It operates on a jQuery object representing a set of form elements.

In your case:

var query = $(this).closest('form').serialize();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.