I am implementing Backbone.js, and I am just trying to understand how the sync function works. To keep it very simple, here is the model.
var Item = Backbone.Model.extend({
defaults: {
name: "Goo"
},
url: "commlink.php"
});
and then
Backbone.sync("create", item);
This is my commlink.php
$item=json_decode($_POST);
$name=$item->name;
$results=$mdb2->query("INSERT INTO list VALUES (NULL, '$name')");
I see a new row show up in my DB, however, the field "name" is blank. I tried both item.save() and the above method...both ended up with the same blank cell but a new entry.
This is the error in chrome in network/content:
<b>Warning</b>: json_decode() expects parameter 1 to be string, array given in ...XXX...
This is in the request payload:
{"name":"Goo"}
-
please have a look at this post stackoverflow.com/questions/5096549/…Rajkamal Subramanian– Rajkamal Subramanian2011年08月02日 05:51:54 +00:00Commented Aug 2, 2011 at 5:51
-
As far as i am aware the $_POST variable is a superglobal array, i.e. it will always be an array.Adam Purdie– Adam Purdie2011年08月02日 22:32:45 +00:00Commented Aug 2, 2011 at 22:32
-
How do you think I can "receive" this request then...as described officially: "Backbone.sync is the function that Backbone calls every time it attempts to read or save a model to the server. By default, it uses (jQuery/Zepto).ajax to make a RESTful JSON request"-Backbone.jsWilliam Sham– William Sham2011年08月02日 22:38:25 +00:00Commented Aug 2, 2011 at 22:38
-
json_decode() will only take a string because thats what it does, it decodes a json string. stackoverflow.com/questions/6207286/… says to use $GLOBALS['HTTP_RAW_POST_DATA']Adam Purdie– Adam Purdie2011年08月02日 22:38:50 +00:00Commented Aug 2, 2011 at 22:38
-
You should also consult the standard SQL injection question here on SO, stackoverflow.com/questions/332365/….El Yobo– El Yobo2011年08月15日 01:56:26 +00:00Commented Aug 15, 2011 at 1:56
3 Answers 3
$rawJSONString = file_get_contents('php://input');
$item = json_decode($wrapperString);
//$item->name is the data you want
8 Comments
$item = json_decode(file_get_contents('php://input'), true);
print_R($item);
Found this is more helpful
Comments
SECURITY NOTE: as pointed out in the comment this is not the way you should ACTUALLY insert the user provided content into your database, this is simply to show you how to get access to the array information as JSON, you should use prepared statements, a framework database adapter, or some other appropriate solution for escaping the user provided content before sticking it into the database.
You're trying to run an array ($_POST) through a function (json_decode) that only accepts a string. The solution in this specific example would be to do this:
$results=$mdb2->query("INSERT INTO list VALUES (NULL, '{$_POST['name']}')");
This would work because you're accessing $_POST as the associative array that it is.
However what I think you actually want to do is first convert the $_POST array to json, then decode it so you can use it the way you wanted to (accessing it as an object, which the json_decode returns):
$item=json_encode($_POST);
$item=json_decode($item);
$name=$item->name;
$results=$mdb2->query("INSERT INTO list VALUES (NULL, '$name')");
For reference: