使用 PHP 将记录保存在 Angular 中时出错


getting error while saving the record in angular with php.

目前我正在用角度.js应用程序做项目。我正在使用服务器作为php。在将记录保存在 Angular js 中时,我遇到了错误。我在下面上传了我的代码。

网页代码:

<div  ng-controller="theaterController">
<div class="col-md-12" ng-if="isCreating">
<form class="form-horizontal" name="saveForm" method="post">
    <h4>Create</h4>
    <p class="danger">{{error}}</p>
    <p class="success">{{msgs}}</p>
    <div class="form-group">
        <label for="theaterName" class="col-md-offset-2 col-sm-2 control-label">Name</label>
        <div class="col-sm-5" style="margin-bottom:10px;">
          <input type="text" name="theatername" class="form-control" id="theaterName" placeholder="Enter theater name" ng-modal="theatername" required>
        </div>
        <div class="col-md-offset-4 col-sm-8">
          <input type="submit" class="btn btn-primary btn-xs" data-ng-click="submitForm()" id="submit">
          <input type="button" class="btn btn-primary btn-xs" ng-click="cancelCreating()" id="Cancel" value="Cancel">
        </div>  
     </div>
</form>

角度.js代码:

app.controller('theaterController', ['$scope', '$http', function ($scope, $http) {
$scope.message = 'Theater';
$http.get('ajax/theater-master.php',{"getElement": "get_data"}).success(    function (response){
    //console.log(response);
    $scope.theaters = response;
});
//save form code;
$scope.submitForm = function (){
    var url = 'ajax/theater-master-save.php';
    var d1 = {'name': $scope.theatername };
     $http.post(url, d1
    ).success(function(data, status, headers, config) {
        console.log(data);
        if (data.msg != '')
        {
            $scope.msgs.push(data.msg);
        }
        else
        {
            $scope.errors.push(data.error);
        }
    }).error(function(data, status) { // called asynchronously if an error occurs
// or server returns response with an error status.
        $scope.errors.push(status);
    });
}

}]);

剧院大师保存.php:

<?php 
$data = json_decode(file_get_contents("php://input"));
$usrname = mysql_real_escape_string($data->name);
echo $usrname ;
$query = mysql_query("INSERT INTO `theater` (theater) VALUES ("' . $usrname . '")");
if($query){
    $arr = array('message' => 'Theater master created successfully', 'error' => '');
    $arrjson = json_encode($arr);
    echo $arrjson;
}else{
    $arr = array('message' => '', 'error' => 'error in inserting record');
    $arrjson = json_encode($arr);
    echo $arrjson;
}
?>

错误:

Uncaught Error: Bootstrap's JavaScript requires jQuery
script.js:52 <br />
<b>Notice</b>:  Undefined property: stdClass::$name in    <b>E:'xampp'htdocs'angular-ticketbooking'ajax'theater-master-save.php</b> on    line <b>3</b><br />
angular.min.js:107 TypeError: Cannot read property 'push' of undefined
at script.js:55
at angular.min.js:87
at angular.min.js:119
at n.$eval (angular.min.js:133)
at n.$digest (angular.min.js:130)
at n.$apply (angular.min.js:133)
at h (angular.min.js:87)
at K (angular.min.js:91)
at XMLHttpRequest.z.onload (angular.min.js:93)(anonymous function) @     angular.min.js:107(anonymous function) @ angular.min.js:80(anonymous function) @    angular.min.js:119n.$eval @ angular.min.js:133n.$digest @   angular.min.js:130n.$apply @ angular.min.js:133h @ angular.min.js:87K @    angular.min.js:91z.onload @ angular.min.js:93

我是角度js的新手,我只是通过尝试角度中的crud操作来练习角度.js。我刚刚尝试了上面的代码,我收到这样的错误。请告诉我我要去哪里。提前致谢

看起来您遇到了两个错误,并且您的 HTML 中有拼写错误:

1("未捕获的错误:Bootstrap的JavaScript需要jQuery">

这是因为 Bootstrap 需要 jQuery,而您尚未将 jQuery 库添加到 HTML 页面。您需要通过添加以下脚本标记在 HTML 页面中包含 jQuery: <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>

2( "类型错误:无法读取未定义的属性'push'">

这是因为您正在尝试将某些内容推送到$scope.msgs中,但Angular没有$scope.msgs,因为您尚未定义它。您需要在控制器中定义 $scope.msgs,然后才能向其添加内容,如下所示: $scope.msgs = []; 您还需要以相同的方式定义$scope.errors

3(使用ng模型而不是ng模式

在你的HTML中,你有一个错别字。您键入的是ng-modal而不是ng-model,这就是值未发送到服务器的原因。您需要将 HTML 更改为: <input type="text" name="theatername" class="form-control" id="theaterName" placeholder="Enter theater name" ng-model="theatername" required>