Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit dynamic form with AJAX? [duplicate]

I am looking to submit a page full of quantities to a page and then return their values. I have the following:

<input type="number" name="item1" />
<input type="number" name="item2" />
<input type="number" name="item3" />
<input type="number" name="item4" />
<input type="number" name="item5" />

AJAX:

function getnames(){
    $.ajax({
          method: 'post','getnames.php',
          data: {
            'order': order,
            'ajax': true
          }
          success: function() {

    });
}
});
}

I want to find a way to post all input fields that contain the string "item" in the name so my question is:

How do I put a variety of input fields into a javascript array and then post them using ajax?

like image 685
William Avatar asked Nov 20 '15 14:11

William


1 Answers

You can use the ajaxForm/ajaxSubmit functions from Ajax Form Plugin or the jQuery serialize function.

AjaxForm:

$("#theForm").ajaxForm({url: 'server.php', type: 'post'})

or

$("#theForm").ajaxSubmit({url: 'server.php', type: 'post'})

ajaxForm will send when the submit button is pressed. ajaxSubmit sends immediately.

Serialize:

$.get('server.php?' + $('#theForm').serialize())

$.post('server.php', $('#theForm').serialize())

AJAX serialization documentation is here.

like image 180
Ajay Makwana Avatar answered Oct 10 '22 02:10

Ajay Makwana