tags:

views:

69

answers:

2

I have an Array in PHP which contains data from database. And it prints out also as a table in the same page which has an AJAX delete function.

Trying to explain better

The array contains debt sums related to many people, it is the application's main function. In the same page, there is a table containing every debt record related to the array, which can be deleted or edited using AJAX.

I have coded the part of deleting the record and removing the TR entry, but it's not enough: I'd like to change also the debt sum using AJAX which is an PHP Array.

What I have

I have the JS function which removes the TR when the delete button is clicked

// TR Fading when deleted
    $('.delete')
        .click(function() {
        $.ajax({
            type: 'GET',
            url: 'history/delete/id/'+$(this).attr('id')
        });
        $(this).parent().parent().fadeOut();
        return false;
        });

and I have the PHP array (image)

+3  A: 

Your question is too vague to give a good answer, but have a look at PHP's json_encode and json_decode functions. These allow you to encode a PHP variable in JSON, which can then be parsed readily by JavaScript and manipulated as a normal JavaScript object. When you've made the changes you want, you can then send it back to PHP via AJAX.

Will Vousden
+3  A: 

I'd suggest returning the PHP array in JSON format. So the output from your script might look something like:

[
    {'debtor': 'Bixo', 'creditor': 'Paulo', 'value': 42.98},
    {'debtor': 'Bixo', 'creditor': 'Otavio', 'value': 12.28},
    {'debtor': 'Luis', 'creditor': 'Bixo', 'value': 10.76},
    {'debtor': 'Luis', 'creditor': 'Paulo', 'value': 248.18},
    {'debtor': 'Luis', 'creditor': 'Otavio', 'value': 49.28},
    {'debtor': 'Otavio', 'creditor': 'Paulo', 'value': 122.89}
]

If you return that with the JSON content type, then many JS libraries (it looks like you're using jQuery?) will automatically parse that into an object - an array of objects in this case.

Matt