我有一些默认值和URL中的Backbone.js的模型:
var Box = Backbone.Model.extend({
url: "./save.php",
defaults: {
x: 0,
y: 0,
w: 1,
h: 1
}
});
然后,我有这个模型的实例,我继续保存它:
var box = new Box({ x:10, y:10, w:200, h:200 });
box.save();
现在我想用PHP脚本“save.php”,它是这样的,以这种模式保存到一个MySQL数据库:
<?php
include('connection.php');
$id = $_POST['cid'];
$x = $_POST['x'];
$y = $_POST['y'];
$w = $_POST['w'];
$h = $_POST['h'];
mysql_query("INSERT INTO boxes (id, x, y, w, h)
VALUES('$id', '$x', '$y', '$w', '$h')
") or die(mysql_error());
?>
echo "Data Inserted!";
我曾尝试阅读很多教程,但我不能让这个简单的模型保存到工作。 为什么不工作我的代码? 如何任何想法可以在此得到解决?
谢谢
编辑:快速的解决方案
在PHP脚本,以获得从发送JSON对象的信息,正确的方法如下:
$box_data = json_decode(file_get_contents('php://input'));
$x = $box_data->{'x'};
$y = $box_data->{'y'};
$w = $box_data->{'w'};
$h = $box_data->{'h'};
并以存储在数据库中:
mysql_query("INSERT INTO boxes(id, x, y, w, h)
VALUES('', '$x', '$y', '$w', '$h') ")
or die(mysql_error());
这样一列将表中的“盒子”与骨干模型箱的属性中的每一个的信息插入。 在这种情况下,服务器请求方法是POST,并在表的“盒子”的ID设置为自动递增。