【发布时间】:2015-09-21 09:54:33
【问题描述】:
我正在处理角度和角度材料(材料设计)依赖性。
我有这个主网格,当我单击它时,将创建一个图块,当我单击该图块时会出现一个弹出窗口。在该弹出窗口中是一个带有 2 个输入字段的表单,这些字段应显示图块的 x 和 y 坐标。这些值不会显示在输入字段中,但会显示在我的控制台中。
我想要实现的是能够将这些坐标保存到我的数据库中。出于某种原因,当我点击保存时,它没有保存到我的数据库中。
这是我的弹出框代码:
<form ng-controller="AppCtrl">
<div layout="row">
<input type="text" id="coord_x" name="coordinate_x" value="" ng-model="task_coordinate_x" >
<input type="text" id="coord_y" name="coordinate_y" value="" ng-model="task_coordinate_y">
</div>
</form>
我的app.js的代码:
app.controller('AppCtrl', function($scope, $mdDialog, $http) {
$scope.save_task = function() {
$http.post('db.php?action=add_task',
{
'task_coordinate_x' : $scope.task_coordinate_x,
'task_coordinate_y' : $scope.task_coordinate_y
}
)
.success(function (data, status, headers, config) {
//$scope.get_task(); //this will fetch latest record from DB
console.log("The task has been added successfully to the DB");
console.log(data);
})
.error(function(data, status, headers, config) {
console.log("Failed to add the task to DB");
});
function snapToGrid(x, y, $element, animate) {
//Set gridsize and offsets
var gridSizeX = 60;
var offsetX = 0;
var gridSizeY = 69;
var offsetY = 10;
//calculate the x and y positions on the grid
var newX = x - ( x % gridSizeX );
var newY = y - ( y % gridSizeY );
// pass the x and x to the popup hidden input field to be able to save to db
localStorage.setItem('coordinateX', newX);
localStorage.setItem('coordinateY', newY);
console.log("NewX: " + newX);
console.log("NewY: " + newY);
//apply the new positions
if(animate){
$element.animate({
top: offsetY + newY,
left: offsetX + newX,
}, 200)
}else{
$element.css({
top: offsetY + newY,
left: offsetX + newX,
})
}
}
};
这是我的 php 代码:
<?php
include('config.php');
switch($_GET['action']) {
case 'add_task' :
add_task();
break;
}
/** Function to add a task to db **/
function add_task() {
$data = json_decode(file_get_contents("php://input"));
$task_coordinate_x = $data->task_coordinate_x;
$task_coordinate_y = $data->task_coordinate_y;
print_r($data);
$qry = 'INSERT INTO tblTask(task_coordinate_x, task_coordinate_y)
VALUES ("'
. $task_coordinate_x . '","'
. $task_coordinate_y . '")';
echo ($qry);
$qry_res = mysql_query($qry);
if ($qry_res) {
$arr = array('msg' => "Task added successfully!!!", 'error' => '');
$jsn = json_encode($arr);
// print_r($jsn);
}
else {
$arr = array('msg' => "", 'error' => 'Error in inserting record');
$jsn = json_encode($arr);
// print_r($jsn);
}
}
?>
【问题讨论】:
-
由于继承和适当的样式,我建议将坐标变量存储在对象
coordinates.x和coordinates.y中。我也会在那里初始化它们。你有没有调查它失败的地方?您的服务器是否获取了变量,或者它们在save_task方法中不可用? -
@enpenax,这就是发生的事情-> recordit.co/HcnJOljPxC
-
可以去掉 print_r() 命令试试吗?我不完全确定这一点,但如果调用 PHP,它不会在 print_r() 之外执行
标签: javascript php angularjs material-design