2

I would like to pass a string that has been serialized by JSON using $.ajax to an external php file to be assigned to a regular array.

Here is what I got so far which isn't showing 0th index of taskContent array.

JQuery code:

$(".json").click(function() {
            var allTaskArr = [];
            $('#appendTask .taskbox').each(function(index,domEle){
            //domEle == this
            allTaskArr[index] = $(domEle).val();
            });
        var allTaskStr = '{"taskContent":'+JSON.stringify(allTaskArr)+'}';

            $.ajax({
                url:'testjson.php',
                type:"POST",
                datatype:'json',
                data:allTaskStr

            });
 });

PHP file:

 $jsonContent = $_POST['taskContent'];
 $taskContent = json_decode($jsonContent,true);         
 echo $taskContent[0];
7
  • Have you made sure you are getting the json in PHP? what does print_r($taskContent); show? Commented Jul 30, 2013 at 13:01
  • use print_r($taskContent); in your php file to confirm that 0 index is there Commented Jul 30, 2013 at 13:10
  • Thanks Alex. I can't seem to get any output from my php file. With alert(allTaskStr); in the JQuery file, I'd get {"taskContent":["text-1","text-2"]}, then I'd assign text-1 and text-2 to index-0 and 1 of the taskContent array in the PHP file. Commented Jul 30, 2013 at 13:16
  • silly question, but it needs to be asked: does your PHP program start with <?php Commented Jul 30, 2013 at 13:39
  • what does print_r($_POST) look like? Commented Jul 30, 2013 at 13:53

2 Answers 2

1

You said:

@Spudley I'm getting 'Undefined Index: taskContent' message.

The reason for this that PHP is not receiving a post variable named tastContent.

And the reason for this is that you are not sending the JSON correctly.

In the jQuery code, you are producing a JSON string, and then putting this in the data element of the ajax call. In fact, the data element is supposed to be given an object, not a json string; it encodes it to json itself, so if you give it json in the first place, the PHP program will receive a double-encoded string -- ie a json string which decodes to another json string.

So instead of this:

var allTaskStr = '{"taskContent":'+JSON.stringify(allTaskArr)+'}';

... you need to send the data as an object, like this:

var allTask = {taskContent:allTaskArr};

or just put it straight into the ajax call:

data : {taskContent:allTaskArr}

Hope that helps get you on the right track and explains what the problem is.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot Spudley. I didn't know that the data element expects an object not a json string! Second thing i did to fix my problem was in my PHP file, I use file_get_contents('php://input') to receive the object.
1

Disregard this post before this edit.

This is your issue:

var allTaskStr = '{"taskContent":'+JSON.stringify(allTaskArr)+'}';

Should be:

var allTaskStr = {"taskContent":JSON.stringify(allTaskArr)};

data:, expects an object and you are sending it a string.

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.