Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery get all input values and create an array (with names)

Hay guys, I'm doing some work where i have a large collection of forms. Seems these aren't 'posted' the traditional way (i.e using a submit button). I need a way to collect all the names and values of all form data when clicking a link (document.location is then used for redirects).

I want names to stay intact so that i can used a simple PHP script to work with the data.

any suggestions?

like image 900
dotty Avatar asked Jul 28 '26 16:07

dotty


1 Answers

Might I suggest Serialize Form to JSON:

$.fn.serializeObject = function()
{
   var o = {};
   var a = this.serializeArray();
   $.each(a, function() {
       if (o[this.name]) {
           if (!o[this.name].push) {
               o[this.name] = [o[this.name]];
           }
           o[this.name].push(this.value || '');
       } else {
           o[this.name] = this.value || '';
       }
   });
   return o;
};

and then you can do:

var formArray = $("#myform").serializeObject();
like image 148
cletus Avatar answered Jul 30 '26 05:07

cletus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!