1

I have the following html code

<form method="POST" action="http://localhost:8000/machines/11" accept-charset="UTF-8" role="form" id="formMachine" class="form-horizontal">
    <div class="tabs-container">
        <ul class="nav nav-tabs">
            <li class="active"><a data-toggle="tab" href="#generalInfo"> <i class="fa fa-comment"></i> General Information</a></li>
            <li class=""><a data-toggle="tab" href="#customMacros"><i class="fa fa-paperclip"></i> Custom Macros</a></li>
        </ul>
        <div class="tab-content">
            <div id="generalInfo" class="tab-pane active">
                <div class="panel-body" style="height:490px;">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">Machine</label>
                        <div class="col-sm-10">
                            <input class="form-control" name="name" type="text" value="machine1">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div id="customMacros" class="tab-pane">
            <div class="panel-body">
                <label>My Macros</label>
                <div style="margin-top :20px;">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <td><strong>MacroName</strong></td>
                                <td><strong>MacroValue</strong></td>
                                <td><strong>Description</strong></td>
                                <td><strong>Action</strong></td>
                            </tr>
                        </thead>
                        <tbody class="tbody">
                            <tr>
                                <td>testmacro</td>
                                <td>this is test maro</td>
                                <td>1</td>
                                <td>
                                    <form action="http://localhost:8000/machines/macro/delete" class="form-horinzontal" id="deleteCustomMacroForm19" method="POST" >
                                        <input  type="hidden" name="customMacroID" value="19">
                                        <input type="hidden" name="_token" value="Ikt1wt7grJskDVY652xYi61G89nyZKhcjMdMSGfG">
                                        <input type="hidden" class="custommacroname" value="testmacro">
                                        <a href="#" id="deleteCustomMacroButton19" class="btn btn-sm btn-white deleteButton"><i class="fa fa-close"></i></a>
                                    </form>
                                </td>
                            </tr>
                            <tr>
                                <td>macro2</td>
                                <td>macro2</td>
                                <td>this is macro2</td>
                                <td>
                                    <form action="http://localhost:8000/machines/macro/delete" class="form-horinzontal" id="deleteCustomMacroForm21" method="POST" >
                                        <input  type="hidden" name="customMacroID" value="21">
                                        <input type="hidden" name="_token" value="Ikt1wt7grJskDVY652xYi61G89nyZKhcjMdMSGfG">
                                        <input type="hidden" class="custommacroname" value="macro2">
                                        <a href="#" id="deleteCustomMacroButton21" class="btn btn-sm btn-white deleteButton"><i class="fa fa-close"></i></a>
                                    </form>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</form>

When the delete button is clicked , the form is being posted. Following is the JS code that does that.

    $('tbody').on('click', 'a.deleteButton', function(e) {

        var buttonID = this.id;
        console.log(buttonID);
        var form = $(this).closest('form')[0];
        console.log(form);
        var formID = form.id;
        console.log("Form ID : "+ formID)
        var macroName = $(this).prev().val();


        // delete machine record
        var message = "Are you sure you want to delete <strong> " + macroName + " </strong> macro ?";
        var params = {title:"Delete Macro ? ", text: message, type:"warning", showCancelButton:true, allowEscapeKey:true,allowOutsideClick:false,confirmButtonText:'Yes'};
        submitAForm(buttonID,formID, params );

    });

Problem The problem that I am facing is whenever the second item is clicked the form iD is working fine and the item is indeed getting deleted, but when the first item is clicked , the form id is all wrong. This is only happening with the first item (its not getting deleted) but for every other item its working fine.

When I click the second item this is what I see (and which are the correct values)

https://infinit.io/_/csfAPG4

However when I click the first item this is what I see

https://infinit.io/_/3hd8X8F

In the second case, the FormID is all wrong. Can you please let me know what am I doing wrong ?

Thanks

Gagan
  • 5,416
  • 13
  • 58
  • 86
  • Don't know if it is related, but when I paste your HTML into the [W3C validator](http://validator.w3.org), it tells me that nested forms are not allowed. – blex Aug 16 '15 at 20:30

2 Answers2

1

First things first : do not nest forms (as explained in detail here : Can you nest html forms?).

Fix this, and you should be all good to go.

Community
  • 1
  • 1
Lucbug
  • 475
  • 6
  • 16
0

here's what I'm thinking. It's choosing the overall form value id: formMachine (first line of your html) because nothing has focus, so that is the closest form. Then on the second click, the button has focus and the nearest form is ok. When the page loads, make sure something in the form has focus.

WhiteHat
  • 59,912
  • 7
  • 51
  • 133
  • You shouldn't nest forms as @Lucbug points out. But if you're going to use `closest`, you still need to initialize focus on something or results will be not guaranteed. – WhiteHat Aug 16 '15 at 20:38