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: "mlink.php"
});
and then
Backbone.sync("create", item);
This is my mlink.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"}
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: "mlink.php"
});
and then
Backbone.sync("create", item);
This is my mlink.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"}
Share
Improve this question
edited Aug 2, 2011 at 22:11
William Sham
asked Aug 2, 2011 at 1:39
William ShamWilliam Sham
13.3k11 gold badges51 silver badges67 bronze badges
5
- please have a look at this post stackoverflow./questions/5096549/… – Rajkamal Subramanian Commented 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 Commented 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.js – William Sham Commented Aug 2, 2011 at 22:38
- json_decode() will only take a string because thats what it does, it decodes a json string. stackoverflow./questions/6207286/… says to use $GLOBALS['HTTP_RAW_POST_DATA'] – Adam Purdie Commented Aug 2, 2011 at 22:38
- You should also consult the standard SQL injection question here on SO, stackoverflow./questions/332365/…. – El Yobo Commented Aug 15, 2011 at 1:56
3 Answers
Reset to default 4$rawJSONString = file_get_contents('php://input');
$item = json_decode($wrapperString);
//$item->name is the data you want
$item = json_decode(file_get_contents('php://input'), true);
print_R($item);
Found this is more helpful
https://coderwall./p/vwvy_a
SECURITY NOTE: as pointed out in the ment 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:
http://php/manual/en/function.json-decode.php
http://php/manual/en/function.json-encode.php
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744853284a4597253.html
评论列表(0条)