Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
Ever heard of Firebug? Will show you what's being posted and what the response is. –  Machavity Jan 14 at 1:47
    
using firebug, I know whats being posted: its this orderArray {"4b0df1da-8b2d-7776-0026-52d0b3cefbfa":"3","161699ae-6db0-43d6-e85b-52ca07767b0‌​f":"1","8da4cfc3-b56d-12da-e34c-52d09ed0b310":"2"} There is abunch of stuff in the respoce but cant see any of the var_dump. –  user794846 Jan 14 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. –  James Jan 14 at 2:24
    
what is the result of var_dump($orderArray); ?? –  Kanishka Panamaldeniya Jan 14 at 2:29
    
var_dump($orderArray); shows null –  user794846 Jan 15 at 9:37

2 Answers 2

can you try change this

var dataString = { 'orderArray':jsonArray };

to

var dataString = { 'orderArray': order };
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.