0

I'm using jQuery to bring up a jQuery UI dialog and in it there is a form. I then use the ajax function of jQuery to submit my form data. The problem lies here....I have a bunch of stuff in a table with a edit button. This edit button is supposed to bring up the jQuery UI dialog so I can edit the fields and submit the changes.

I make my changes and then during the submit, it submits data from the first link in my table.

Here is how my JS code looks

$('.edit_task').each(function() {
var $link = $(this);
var $dialog = $('<div></div>')
    .load($link.attr('href'))
    .dialog({
        autoOpen: false,
        title: "Edit Task",
        width: 700,
        height: 550,
        modal: true,
        buttons: {
            "Save": function() {
                $.ajax({
                    url: $link.attr('href'),
                    type: "POST",
                    data: $("#EditTaskForm").serialize(),
                    dataType: "html",
                    async: true,
                    cache: false,
                    error: function()
                    {
                        alert("Error: An error occured while trying to update a task.");
                    },
                    success: function()
                    {

                        $(this).dialog('close');
                        location.reload();
                    }
                });
            },
            "Cancel": function () { $(this).dialog('close'); }
        }
});

$link.click(function() {
    $dialog.dialog('open');

    return false;
});
});

I've been trying to fix this problem for days and I can't seem to find out what the problem is.

Edit: Here is the form HTML http://pastebin.com/knh1AVGk

8
  • 1
    We need to see your form html. Commented Jan 24, 2012 at 20:46
  • 2
    Do you have multiple forms with id="EditTaskForm"? Commented Jan 24, 2012 at 20:46
  • @KevinB it's one view file with the id="EditTaskForm" that shows up when I click the edit button. The controller class handles what goes inside the form. Commented Jan 24, 2012 at 20:47
  • @Diodeus here you go: pastebin.com/knh1AVGk Commented Jan 24, 2012 at 20:48
  • Please don't post your PHP code, just the HTML output. This all happens on the client. Commented Jan 24, 2012 at 20:51

3 Answers 3

1

The culprit is this line

var $dialog = $('<div></div>')

Whenever you click on edit link the above line is creating a new div and loading the form inside which is not appended to a body but jQuery still keeps it inside document fragment. So next time when you click on edit link a new div is created and the same form is loaded again in the document fragment. So now there are multiple edit forms on the page with same id and when you use $("#EditTaskForm").serialize() it will always get the first forms data.

The solution is, you should maintain a div with some id or class to load the form in the dialog box. Try this code.

$('.edit_task').each(function() {
    var $link = $(this);

    //This part of the code will fix the issue
    var $formContainer = $('#editFormContainer');
    if($formContainer.length == 0){
        $formContainer = $('<div id="editFormContainer"></div>')
                         .appendTo(document.body);
    }

    console.log($("#EditTaskForm").length);

    var $dialog = $formContainer
        .load($link.attr('href'))
        .dialog({
            autoOpen: false,
            title: "Edit Task",
            width: 700,
            height: 550,
            modal: true,
            buttons: {
                "Save": function() {
                    $.ajax({
                        url: $link.attr('href'),
                        type: "POST",
                        data: $("#EditTaskForm").serialize(),
                        dataType: "html",
                        async: true,
                        cache: false,
                        error: function()
                        {
                            alert("Error: An error occured while trying to update a task.");
                        },
                        success: function()
                        {

                            $(this).dialog('close');
                            location.reload();
                        }
                    });
                },
                "Cancel": function () { $(this).dialog('close'); }
            }
    });

    $link.click(function() {
        $dialog.dialog('open');

        return false;
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this code but for some reason I get the error message I put in.
Nevermind, the error message was something on my end, but the prolem is still not solved.
You mean it is still submitting the old form data?
I have edited the answer and added console.log($("#EditTaskForm").length);. Can you check what it says after couple of edits?
1
var idCounter = new Date().getTime();
var customDialog = $('<span id='+ (idCounter++) +' ></span>')  <- unique Id
.dialog({....
 .
 .
 .
 buttons:[{
   text: save
   click: function() {
      $.ajax({
       type:'POST'
    url: '/config/update_services/',
        dataType: 'json',
        cache: false,
        data: $('#form').serialize(),
        success: function(data) {
            $('#form').remove();                  <--- this helps will remove the form making sure the new form is there
            customDialog.dialog( 'close' );
            /// do otherstuff
    },       
     })
   }

}]  

})

Comments

0

Try this:

Where you do

var $dialog = $('<div></div>') 

give that an Id e.g

var $dialog = $('<div id="myDiv"></div>') 

then try this in the ajax bit:

data: $("#myDiv").find('form').serialize()

6 Comments

Another issue maybe that the when you close the dialog, it doesn't get removed from the DOM. Try a this.remove() in a close event on the dialog
e.g. close: function() { $(this).remove(); }
Just looking at the HTML, why is there a table in EditTaskForm? You shouldn't be using tables for layout
Just so that everything looks spaced out evenly. The website is all done in CSS/HTML5 + its an internal website so we just care about getting the product working right now and then worrying about the layout and what not.
Ok, I know it's slightly off topic but it is nevertheless bad practice and should be avoided. You can space out evenly with plain CSS. Anyway, do you have the HTML for the actual table with the edit button that opens up the form mentioned above?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.