Many forms, but only imagine one that changes

What have i done so far

I created a website using CGI::Applicationand HTML::Templatesin Perl, which looks like this .

The bottom form will appear for each data set that is in my database. For instance. 400 times.

The idea of ​​the lower form is that the user should be able to make changes to the existing data set, where the upper form creates a new data set.

The top form works and uses Ajax to search the database for invalid values ​​that the user is not allowed to enter.

Problem

On each data set, the "Save" button will appear, as shown in the screenshot.

How to send only the data set corresponding to the "Save" button that was pressed using Ajax?

I can do it using pure jquery and have it hidden inputso

<form action="" method="post">
   <input name="anchor" value="34" type="hidden">
   <input name="title" type="text" />

   ...

   <button  type="submit">Save</button>
</form>

but without Ajax, I cannot first check for invalid values ​​in the database.

What is the Ajax code that will only represent the data set for the form to which the save button belongs?

+3
source share
1 answer

You can use the method .serialize():

$('form').submit(function() {
    $.ajax({
        url: this.action,
        type: this.method,
        data: $(this).serialize(),
        success: function(result) {
            // the AJAX request succeeded => do something
            // with the results returned by the server
        }
    });
    return false;
});

This will be subscribed to the event of sending all forms (perhaps you can configure the selector to restrict it to only the forms contained in the table), and when sending it, it cancels the default action and sends an AJAX request, serializing all form fields.

+2
source

All Articles