Php $scope don';我不能在$http.post上工作

Php $scope don';我不能在$http.post上工作,php,angularjs,Php,Angularjs,当我想将数据插入mysql时,我的脚本有问题。 一切正常,但$scope不会将数据发送到post.config 以下是控制器的代码: app.controller("addNewsCtrl", function($scope, $http) { $scope.postData = function($scope) { $http.post("php_libs/add_news.php", { 'newstitle':$scope.newstitle

当我想将数据插入mysql时,我的脚本有问题。 一切正常,但$scope不会将数据发送到post.config

以下是控制器的代码:

    app.controller("addNewsCtrl", function($scope, $http) {
  $scope.postData = function($scope) {
        $http.post("php_libs/add_news.php", {
          'newstitle':$scope.newstitle,
          'today':$scope.today,
          'content':$scope.content
        }).then(function(response) {
          console.log(response.statusText);
        });
      };
});
和模板中的代码:

<div>
  <form>
    <div class="form-group">
      <?php $today = date("d-m-Y"); ?>
      <label>Tytuł:</label>
      <input type="text" ng-model="newstitle" placeholder="Wpisz tytuł" class="form-control">
    </div>
    <div class="form-group">
      <label>Data:</label>
      <input type="date" ng-model="today" class="form-control">
    </div>
    <div class="form-group">
      <label>Treść: </label>
      <textarea ng-model="content" placeholder="Wpisz treść posta..." class="form-control"></textarea>
    </div>
    <div class="form-group">
      <button class="btn btn-primary btn-lg btn-block" ng-click="postData()">Dodaj post</button>
    </div>
  </form>
</div>
但是console.log仍然不返回任何内容

PHP代码

flag    

<?php include("connect.php"); 
$data = json_decode(file_get_contents("php://input")); 
$title = mysql_real_escape_string($data->title); $date = mysql_real_escape_string($data->post_date); 
$text = mysql_real_escape_string($data->post_text); 
$sql = "INSERT INTO news (title, post_date, post_text) VALUES ('" . $title . "', '" . $date . "', '" . $text . "')"; 
$res = $conn->query($sql); ?>
标志

从方法中删除参数
$scope
,因为
ng click=“postData()”
不会将scope作为第一个参数传递。因此它将在
postData
方法的本地上下文中未定义:

$scope.postData = function() {
    $http.post("php_libs/add_news.php", {
      'newstitle':$scope.newstitle,
      'today':$scope.today,
      'content':$scope.content
    }).then(function(response) {
      console.log(response.statusText);
    });
};

postData()
函数中删除
$scope

或者将
关键字传递给ng click中的函数,如
ng click=“postData(this)”

您已经在控制器中实现了Post功能,$scope是一个具有控制器可用属性和方法的对象。所以,在post函数中已经有了控制器的作用域,不需要将其作为参数传递

你的代码应该是这样的

    app.controller("addNewsCtrl", function($scope, $http) {
  $scope.postData = function() {
        $http.post("php_libs/add_news.php", {
          'newstitle':$scope.newstitle,
          'today':$scope.today,
          'content':$scope.content
        }).then(function(response) {
          console.log(response.statusText);
        });
      };
});
我解决了这个问题

所有问题都是由mysql\u real\u escape\u string()引起的

这非常有效:

<?php
  include("connect.php");
  $data = json_decode(file_get_contents("php://input"));

  $title = $data->postTitle;
  $date = $data->postDate;
  $content = $data->postContent;

  $sql = "INSERT INTO news (post_date, post_title, post_content) VALUES ('$date', '$title', '$content')";

  $res = $conn->query($sql);

?>

尝试删除post函数中的
$scope
,除非您显式传递它-
$scope.postData=function(){….
您的文件中有更多的php代码吗?还是仅此而已?这是我用于此任务的全部代码。我尝试了。效果相同。您的意思是$scope仍然未定义?现在$scope没有值。控制台中为空行。所有人。控制台中仍为空行。$scope没有发送数据,数据库中有空记录:)将范围放在控制台上,让m您知道$scope是否为空吗?
<?php
  include("connect.php");
  $data = json_decode(file_get_contents("php://input"));

  $title = $data->postTitle;
  $date = $data->postDate;
  $content = $data->postContent;

  $sql = "INSERT INTO news (post_date, post_title, post_content) VALUES ('$date', '$title', '$content')";

  $res = $conn->query($sql);

?>