0

I am trying to edit a data query using php by passing it through javascript,

my ajax request looks like

var totalSearchResult=10;
$.ajax({
    url:"php/queryManipulation.php",
    type: 'POST',
    data: { totalQuery : totalSearchResult,  query : '{"data":{"match_all":{}}}'},

    success: function(finalList)
    {
        alert(finalList);
    }
});

my php code looks like

<?php
$from=$_POST["totalQuery"];
$qry=json_decode($_POST["query"]);
$qry->from=$from;   }?>

I am trying to get it in the form,

{"data": {"match_all": {}} , "from": 10}

I get the error Object of class stdClass could not be converted to string

2
  • 1
    You forgot to json_encode it back? Commented Jul 24, 2012 at 8:34
  • And why I'd see an extra } at the end of your PHP script? Commented Jul 24, 2012 at 8:36

3 Answers 3

3

Edit: Changed json_decode return value from array to object

You need to encode the json again just after finishing the edits. So what you can do is something like:

<?php
    $from            = $_POST["totalQuery"];
    $qry             = json_decode($_POST["query"]);
    $qry->data->from = $from;
    //you will get the new json string 
    //as the finalList variable in your post callback
    echo json_encode($qry); 
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I get it in the form {"data":{"match_all":[]},"from":"10"}... It should be like{"data":{"match_all":{}},"from":"10"}
I've changed the answer. Try now.
0

You should use json_decode($string, true) - so it will be an array. Details here: http://php.net/manual/en/function.json-decode.php

Comments

0

You can decode to an array (not sure about objects) and re-encode:

$qry = json_decode($_POST['query'], TRUE);
$qry['from'] = 10;
$new_qry = json_encode($qry);

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.