dptgpyl61857413
dptgpyl61857413
2016-08-23 05:49

尝试使用Angularjs更新数据库中的数据

  • php
  • angularjs
  • javascript

I am trying to update data in the database using angular in Laravel 4 but the data is'nt get inserted, and a blank value is getting stored in to the database. Please find the bug and try to insert the passed value insteed of blank value.

JS File

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 File

<!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>

Controller

<?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]);
    }
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐