1

i try to send data to my php scripts but i think i miss something.

First of all,

   function deleteData2()
    {
        var artistIds = new Array();

        $(".p16 input:checked").each(function(){
            artistIds.push($(this).attr('id'));
        });


       $.post('/json/crewonly/deleteDataAjax2', JSON.stringify({'artistIds': artistIds}),function(response){
        if(response=='ok')
            alert(artistIds);
    });


    }

Above code is my js file. i have artistIds in var artistIds. My goal is sending this array to my php script.In order to that, i make it json , i mean encode it with JSON.stringify

then in php side, i use below code.However, $array is always null. What might be the reason ?

public function deleteDataAjax2() {

        $array=json_decode($_POST['artistIds']);

        if (isset($array))
            $this->sendJSONResponse('ok');

    }
2
  • 1
    Have you tried print_r( $_POST )? It should be the #1 step in any situation like this. Commented Mar 25, 2012 at 8:04
  • Sorry, but I don't believe you; $_POST is always an array, even if no post data is sent. (Don't print $array but the whole $_POST variable.) Commented Mar 25, 2012 at 8:08

1 Answer 1

4

You are passing the data as a raw string of JSON, but your PHP is trying to find that string by parsing the data as application/x-www-form-urlencoded and then looking at the artistIds key.

Assuming the array is flat: Forget JSON. You don't need it.

$.post('/json/crewonly/deleteDataAjax2', {'artistIds': artistIds},function(response){

And:

$array = $_POST['artistIds'];

If the array isn't flat, then:

$.post('/json/crewonly/deleteDataAjax2', 
       { json: JSON.stringify({'artistIds': artistIds}) },
       function(response){

And (with suitable error checking added):

$json = $_POST['json'];
$data = json_decode($json);
$artists = $data['artistIds'];
Sign up to request clarification or add additional context in comments.

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.