JQuery Form plugin or jQuery serialization?

I am wondering what are the benefits of making the jQuery Form plugin more Serialize?

If I prefer to use the form plugin, can I use the new jQuery 1.5 features?

Also, is there anything in the forms plugin to stop multiple backlinks? I usually use the Ajax manager plugin to stop duplicate ajax posts, but I don't think I can use it with a form plugin.

thanks

+4
source share
5 answers

The jQuery form plugin offers the ease of use of ajax with forms. He will use the attributes of the form to determine how and where to submit the form. The form method attribute tells the plugin to use the request type. The action attribute of the form tells where the form is submitted.

Given an example of this form:

<form id="myform" action="submit.php" method="post"> <!--inputs and submit button go here--> </form> 

In essence, this allows you to write:

 $('#myform').ajaxForm(); 

instead

 $.post("submit.php", $("#myform").serialize()); 

The jquery Form plugin will also allow you to submit code through Ajax.

 $('#myform').ajaxSubmit(); 

The jQuery Forms plugin will serialize the form anyway, and you will have to serialize before submitting it to the server. The jQuery Form plugin simply serializes the form behind the scenes. The above examples do not process any responses from the server.
Using the code below, you can add an answer to elements whose class attribute contains a β€œresult”.

 $('#myform').ajaxForm({ target : ".result"}); 

You can use any selector that is supported in jQuery for the target option.

I'm not sure if you can use deferred methods or not. I doubt it because compatibility is for 1.3.2+, and the ajax built-in methods are wrapped in a plugin. So the Deferred object never returns from the plug-in's internal modules.

The jQuery form has a beforeSubmit parameter that you can use. So, adding to the above code, we get:

 $('#myform').ajaxForm({ target: ".result", beforeSubmit: function(arr, $form, options) { // arr: The array of form data // The array of form data takes the following form: // [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] //some code to check if we already submitted the form // return false to cancel submit } }); 

However, I would recommend not to do this if the user has finished the page after submitting the form. This would add unnecessary complexity to your client code. It should be nice if the user will do other things on the page after submitting the form. The jQuery Forms plugin has a successful option, which is called by a function call if the server returns a 200 "OK" response. You can also check the authors website for the plugin http://jquery.malsup.com/form/ for more information about the plugin.

+3
source

Ive just looked into the forms plugin, and to be honest, if you have a little experience using jquery, there is no reason to use the plugin. I do not see anything that you cannot achieve, simply using simple jquery. If I need to submit my data in full form using ajax, I use serialization.

Is that all you need for a basic ajax call, what can the forms plugin do to make it easier?

 $.ajax({ type: 'POST', url: post/to/this/url, data: $("#myForm").serializeArray(), success: function() { // Woopie success! window.location.href = successurl; }, error: function(jqXHR, textStatus, errorThrown){ // do something with errors } }); 
+3
source

If you want to upload files using Ajax, this plugin is a must. Looking at the serializeArray documentation, he clearly states that:

Data from file selection items not serialized

This plugin handles file uploads without any problems.

+2
source

You don't need a form plugin when the task is as simple as serializing and submitting a form. A plugin can contain many features that you can never use or never use. Also, if you know that jQuery knowledge is limited or you are not very familiar with how plugins are created, it is not an easy task to configure it the way you need. I go to the plugin, the task is too complicated to create, in which case why reinvent the wheel if there is already a plugin for it. An example is the color picker with HSB controls. If you do not know how colors work, then the time and effort to conduct research and create your own set of colors will be higher.

You can use the following 2 lines as a basic approach for serializing and submitting a form.

 var data = $(form).serialize(); $.post('post.php', '&'+data); 

More on $ .post () at http://api.jquery.com/jQuery.post/#example-3

Now, if you feel that you need to do complex things with your form, and these functions are already available in some kind of plugin, it will not hurt to try it and see how it works for you.

+1
source

The form plugin is useful in that you can send the form via ajax in one call with the same capabilities as $ .ajax. This is a great way to just capture an interface to improve its behavior. This is also good because it smoothly handles file uploads using faux-ajax (iframe) without the need to change anything.

As far as I know, the form plugin does nothing to avoid multiple submissions. I use the plugin for a very large project for different things, and I usually deal with it myself with some additional code to keep track of whether the form is in the middle of the view and not re-enter the function, if any.

0
source

Source: https://habr.com/ru/post/1341564/


All Articles