0

I am trying to create an associative array with the record id as the key and order as the value. I then want to pass this via ajax to php where I will foreach through the array and update the records. But it is not working I seem to be getting null at json_decode($_REQUEST['orderArray'], true);

Whats wrong with the code:

jquery :

 //Make project task table rows sortable
$('#Task_table tbody').sortable({
    items: "tr:not(.disable_sort)",//disable sortable on header row
    helper: fixHelperModified, //call helper function
    update: function(event, ui) {
        var order = {};//create object
        $('#Task_table tr').each(function(){//loop through rows
            var id = $(this).children('td:first-child').find(".order_number").attr("rel");
            var order_number = $(this).children('td:first-child').find(".order_number").val();
            //fill object array with keys(task->id) and values (task->order_number)
            order[id] = order_number;
        });

        //convert array to json
        var jsonArray = JSON.stringify(order);
        //prepare POST data
        var dataString = { 'orderArray':jsonArray };

        $.ajax({
            type: "POST",
            url: "index.php?module=Project&action=update_order",
            data: dataString,
            success: function() {
               // location.reload();
            }
        });
    }
});

this sends via post: orderArray {"4b0df1da-8b2d-7776-0026-52d0b3cefbfa":"3","161699ae-6db0-43d6-e85b-52ca07767b0f":"1","8da4cfc3-b56d-12da-e34c-52d09ed0b310":"2"}

The php:

//updates the order of the tasks
function action_update_order(){
    //create object/array from json data
    $orderArray = json_decode($_REQUEST['orderArray'], true);

    var_dump($orderArray);

    foreach($orderArray as $id => $order_number){

        $GLOBALS['log']->fatal('order: '.$order_number[$id]);

        $task = new ProjectTask();
        $task->retrieve($id);
        $task->order_number = $order_number;
        $task->save();
    }
}

As I said I cant seem to foreach through the result of the jasondecode. Also hard to debug as its ajax.

5
  • 1
    Ever heard of Firebug? Will show you what's being posted and what the response is. Commented Jan 14, 2014 at 1:47
  • using firebug, I know whats being posted: its this orderArray {"4b0df1da-8b2d-7776-0026-52d0b3cefbfa":"3","161699ae-6db0-43d6-e85b-52ca07767b0f":"1","8da4cfc3-b56d-12da-e34c-52d09ed0b310":"2"} There is abunch of stuff in the respoce but cant see any of the var_dump. Commented Jan 14, 2014 at 2:09
  • Using Console.log() you can print to your developer console, such as Firebug. You can print objects, arrays, and primitive data types. Commented Jan 14, 2014 at 2:24
  • what is the result of var_dump($orderArray); ?? Commented Jan 14, 2014 at 2:29
  • var_dump($orderArray); shows null Commented Jan 15, 2014 at 9:37

2 Answers 2

1

can you try change this

var dataString = { 'orderArray':jsonArray };

to

var dataString = { 'orderArray': order };
Sign up to request clarification or add additional context in comments.

Comments

0

For some reason JSON.stringify(order) is adding the Html entity version of " to my string so I need to use htmlspecialchars_decode(); in my php first before json_decode. It seems to work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.