holyredbeard
holyredbeard

Reputation: 21198

Getting jQuery array into php form

I have a form that includes several input text fields and a list that's sortable (se code below). The function below retrieves the new positions that's saved to an array (order). However, what I want is to include the array in the form so that when the user is finished with the sorting, has filled in the text fields and submits the form this array will be apart of the form.

So, how could I get this array into the form with AJAX?

$('#listElements').sortable({
            //revert: true,
            update: function(event, ui) {

                var order = [];
                $('.listObject li').each(function (e) {
                    order.push($(this).attr('id'));
                });
                $.ajax({
                    url: "/index.php?type=list&action=showList&listId=1",
                    type: "post",
                    data: {
                        order_data: order
                    }
                }).success(function (data) {
                    console.log(data);
                });
            }
        });

NOTE: To make it clear, I want to get the array into the form.

Upvotes: 0

Views: 113

Answers (1)

Soundz
Soundz

Reputation: 1300

send it as a JSON encoded string and decode it serverside. As far as I know jquery has a array to JSON function and php got json_decode()

Upvotes: 1

Related Questions