Michelle
Michelle

Reputation: 570

Ajax call not passing any POST values

I have a table that has sortable rows. The table is generated from a mysql database that has a field called "displayorder" that oders the table. When the user drags and drops rows, I want to use AJAX to submit those changes to the database whenever a user drops a row.

Currently, I can see the console.log() output from the success part of the AJAX call, and when i output the data there (order) it looks great, like this: ["order_1=1", "order_2=2", "order_4=3", "order_3=4"]

But according to Firebug, all that's getting passed in the $_POST is "undeclared". How do I access that order variable from my indexpage_order.php file?

I have this jquery code:

    <script>
    $(document).ready(function() {
        var fixHelper = function(e, tr) {
            var $originals = tr.children();
            var $helper = tr.clone();
            $helper.children().each(function(index)
            {
              $(this).width($originals.eq(index).width())
            });
            return $helper;
        };
        var sortable = $("#sort tbody").sortable({
        helper: fixHelper,
        stop: function(event, ui) {
            //create an array with the new order
            order = $(this).find('input').map(function(index, obj) {
                var input = $(obj);
                input.val(index + 1);
                return input.attr('id') + '=' + (index + 1);
            });
            $.ajax({
                    type: 'POST',
                    url: 'indexpage_order.php',
                    data: order,
                error: function() {
                    console.log("Theres an error with AJAX");
                },
                success: function(order) {
                    console.log("Saved.");
                    console.log(order);
                }
            });
            return false;
        }
        });
    });
    </script>

indexpage_order.php contains:

if(isset($_POST) ) {

    while ( list($key, $value) = each($_POST) ) {
        $id = trim($key,'order_'); //trim off order_
        $sqlCommand = 
            "UPDATE indexpage 
             SET displayorder = '".$value."' 
             WHERE id = '".$id."'";
        $query = mysqli_query($myConnection,$sqlCommand) or die (mysqli_error($myConnection));
        $row = mysqli_fetch_array($query);
    }
}

Upvotes: 0

Views: 292

Answers (5)

RyanS
RyanS

Reputation: 78

It looks like your sending your order in a JavaScript array

And when it arrives at your PHP, its unreadable.

If its an object look into the json_decode function. If its an array, serialize the data before it goes, then unserialize on the php side.

PHP is unable to understand JavaScript array or objects unless they are encoded/serialized correctly.

Upvotes: 0

Muthu Kumaran
Muthu Kumaran

Reputation: 17900

order is an array. Convert the order to query string like this in your ajax script,

data: order.join('&'),

Upvotes: 0

HungryCoder
HungryCoder

Reputation: 7616

You can simply rewrite the js code that are generating the data for POST.

order = {}
$(this).find('input').map(function(index, obj)
  { 
    return order[this.id] = index;
  }
)

Rest should work in PHP.

Upvotes: 1

Teena Thomas
Teena Thomas

Reputation: 5239

convert the js array order to json object as ajax data needs to be in json using JSON.stringify. hence, it needs to be data:JSON.stringify(order), in the ajax function.

Or, use json_encode like this data: <?php echo json_encode(order); ?>, in the ajax function

Upvotes: 0

jdstankosky
jdstankosky

Reputation: 657

Try $_REQUEST instead of $_POST on the PHP file for POST method with ajax.

Upvotes: 0

Related Questions