Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Ajax POST HTML form with elements array using jQuery

My Form looks like this:

<form id="invite-form" method="post" action="" >
 <input type="text" name=friends[0][first_name] />
 <input type="text" name=friends[0][last_name] />
 <input type="text" name=friends[0][email] />

 <input type="text" name=friends[1][first_name] />
 <input type="text" name=friends[1][last_name] />
 <input type="text" name=friends[1][email] />

 <input type="text" name=friends[2][first_name] />
 <input type="text" name=friends[2][last_name] />
 <input type="text" name=friends[2][email] />

 <input type="submit" value="Invite" />
</form>

And here is my javascript code:

var friends = [];
    jQuery("#invite-form").serializeArray().map(function(x){friends[x.name] = x.value;});
    var formData = {
        'friends'    : JSON.stringify(friends),
        'action'   : 'invite-friends'
    };
    jQuery.ajax({
        type        : 'POST',
        url         : '/invitation.php',
        data        : formData,
        dataType    : 'json',
        encode      : true
    }).done(function(data){
        console.log(data);
        if(data.success == true){
        ...

It doesn't work properly and the list of friends is not POSTed properly. How do I convert this kind of form to json data properly? I want to be able to use this data as a regular array, as if it were a regular form POSTed.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In this way you can submit form with serialize data and you many use post or get request for this or any other method.

jQuery("#invite-form").submit(function(e) {
   e.preventDefault();
   let data = jQuery(this).serialize();
    jQuery.ajax({
                type: "get",
                url: 'invitation.php',
                data: data,
                cache: false,
                success: function(data){
                    console.log(data);
                }
            });
 });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!