javascript - 输入字段的值显示在控制台中但不在输入字段中+无法保存到数据库

标签 javascript php angularjs material-design

我正在处理 Angular 和 Angular Material ( Material 设计)依赖性。

我有这个主网格,当我单击它时,将创建一个图 block ,当我单击该图 block 时,会出现一个弹出窗口。在该弹出窗口中有一个带有 2 个输入字段的表单,应显示图 block 的 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);
        }
    }
?>

最佳答案

我认为问题可能发生在 php 中

在您的地址栏中输入: http://yourservername.com/db.php/action=debug

制作一个php函数

 switch($_GET['action'])  {

        case 'debug' :
            debug(); 
            break;
    }

function debug() {

$qry = 'INSERT INTO tblTask(task_coordinate_x, task_coordinate_y) 
            VALUES ("' 
             some value '","' 
            some value '")'
    or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR

);}

并确保它的工作

希望对你有帮助。

关于javascript - 输入字段的值显示在控制台中但不在输入字段中+无法保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31208797/

相关文章:

javascript - 合并两个不同大小的相交圆并找到加权半径中心的坐标

php - 网站大,用户多,如何唯一识别用户

php - 匹配数组中的字符串 strpos() : Empty needle

php - mysql中传递键值

javascript - 为什么这些 AngularJS 应用程序不能一起运行?

javascript - 连续更改每个其他 div 的背景颜色

javascript - 在 JavaScript 中 chop 对象的最快方法

javascript - 通过叠加背景可以看到 Bootstrap 事件选项卡

css - DataTables 列标题都挤在第一列

javascript - 显示/隐藏表单元素的问题