【发布时间】:2016-12-29 19:35:00
【问题描述】:
我正在尝试使用 Laravel 4 中的 Angular 更新数据库中的数据,但数据没有被插入,并且一个空白值被存储到数据库中。请找出错误并尝试插入传递的值而不是空白值。
JS 文件
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$scope.updateFunction = function(updateId) {
$http.get("http://localhost/crud/public/registration_update_page/" + updateId).then(function successCallback(response) {
console.log('successCallback');
console.log(response);
alert("Row with id " + updateId + ", updated..!");
}, function errorCallback(response) {
console.log('errorCallback');
console.log(response);
});
};
});
.PHP 文件
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<h2>Registration</h2><br/>
<table border=1>
<tr><td>Name:</td><td><input type="text" ng-model="nam" name="nam"><br/></td></tr>
<tr><td>Email:</td><td><input type="email" ng-model="email" name="email"><br/></td></tr>
<tr><td>Password:</td><td><input type="password" ng-model="password" name="password"><br/></td></tr>
<tr><td>City:</td><td><input type="text" ng-model="city" name="city"><br/></td></tr>
<tr><td><button ng-click="insertFunc()" type="submit">Submit</button></td></tr>
</table>
</div>
</form>
<table style="width:100%" border=1>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Password</th>
<th>City</th>
<th>Update</th>
<th>Delete</th>
</tr>
<tr ng-init='show = true' ng-repeat="x in query" ng-hide='!show'></div>
<!-- ng-hide will work when it is true in condition -->
<td>{{x.id}}</td>
<td><input type="text" value="{{x.name}}" name="edited_name"></td>
<td><input type="text" value="{{x.email}}" name="edited_email"></td>
<td><input type="text" value="{{x.password}}" name="edited_password"></td>
<td><input type="text" value="{{x.city}}" name="edited_city"></td><br/>
<td><input type="submit" ng-click="updateFunction(x.id);" value="Update"></td>
<td><button ng-click="deleteFunction(x.id);show = false">Delete</button></td>
<!-- !show==false means !false i.e., true -->
</tr><div>
</body>
</html>
控制器
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use Session;
use Redirect;
class NewController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
public function registration_update_function(Request $request)
{
$updateId = $request->id;
$edited_name = $request->edited_name;
$edited_city = $request->edited_city;
$users1 = DB::table('angular_registration')->where('id', '=', $updateId)->update(['city' => $edited_city]);
}
}
【问题讨论】:
-
你能在检查中看到 ajax 调用的响应吗?
-
不,只有 ID 在那里显示为“Object {data: " 71", status: 200, config: Object, statusText: "OK"}"
-
你还需要传递名字和城市,对吧?
-
感谢您的努力
-
我对 POST 方法所做的事情与您在 AngularJS 中使用 GET 方法所做的事情相同。最好使用post方法。
标签: javascript php angularjs laravel-4