2016-08-19 221 views
0

我正嘗試在Laravel4中使用ANGULARJS將數據插入到數據庫中。但數據不會進入數據庫。沒有錯誤顯示在這裏。在這裏,我還包括在PHP中顯示數據的foreach循環。使用Laravel4中的ANGULARJS將數據存儲在數據庫中

這裏是查看網頁(*。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>Calculator</h2> 
<form action="calculative_page" method="post"> 
a: <input type="number" ng-model="a" name="a"> 
b: <input type="number" ng-model="b" name="b"> 
<button ng-click="addFunc()">Sum</button> 
<b>Summation : {{sum}}</b> 

<?php 
if(isset($select)) 
{ 
    foreach ($select as $sel) { ?> 
    <input type="text" value="<?php echo $sel->addition ?>"></br> 
<?php }} ?> 

<br/><br/><br/> 
c: <input type="number" ng-model="c" name="c"> 
d: <input type="number" ng-model="d" name="d"> 
<button ng-click="subFunc()">Sub</button> 
<b>Subtraction: {{sub}}</b> 


<?php 
if(isset($select)) 
{ 
    foreach ($select as $sel) { ?> 
    <input type="text" value="<?php echo $sel->subtraction ?>"></br> 
<?php }} ?> 

</div> 
</form> 
<script> 
var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope,$http) { 
    $scope.sum = 0; 
    $scope.addFunc = function() { 
     $scope.sum = $scope.a+$scope.b; 
     $scope.a = $_POST['a']; 
     $scope.b = $_POST['b']; 
     } 
    } 

    $scope.sub = 0; 
    $scope.subFunc = function() { 
     $scope.sub=$scope.c-$scope.d; 
     $scope.c = $_POST['c']; 
     $scope.d = $_POST['d']; 
    } 
}); 
</script> 
</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 calculative_function(Request $request) 
    { 
     echo $a_value = $request->a; 
     echo $b_value = $request->b; 
     echo $c_value = $request->c; 
     echo $d_value = $request->d; 
     if($a_value) 
     { 
     $calc = DB::table('calculation')->insert(['a' => $a_value, 'b' => $b_value,'addition'=>$a_value+$b_value]); 
     return redirect('homework')->with('status','Done..!'); 
     } 
     else 
     { 
     $calc = DB::table('calculation')->insert(['c' => $c_value, 'd' => $d_value,'subtraction'=>$c_value-$d_value]); 
     return view('homework'); 
     } 

    } 

    public function homework_function(Request $request) 
    { 
     $select = DB::select('select addition, subtraction from calculation'); 
     return view('homework',['select' => $select]); 
     //return view('homework'); 
    } 

這裏是route.php

Route::get('/homework', '[email protected]_function'); 

Route::get('/calculative_page', '[email protected]_function'); 

回答

0

我解決了它我的自我,沒有更改爲Route.phpController。唯一的變化是在視圖頁面。請查看代碼,如下圖所示:

<!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>Calculator</h2> 
<form action="calculative_page" method="get"> 
a: <input type="number" ng-model="a" name="a"> 
b: <input type="number" ng-model="b" name="b"> 
<button ng-click="addFunc()">Sum</button> 
<b>Summation : {{sum}}</b> 
</form> 

<br/><br/><br/> 
<form action="calculative_page" method="get"> 
c: <input type="number" ng-model="c" name="c"> 
d: <input type="number" ng-model="d" name="d"> 
<button ng-click="subFunc()">Sub</button> 
<b>Subtraction: {{sub}}</b> 

</div> 
</form> 
<script> 
var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope,$http) { 
    $scope.sum = 0; 
    $scope.addFunc = function() { 
     $scope.sum = $scope.a+$scope.b; 
     } 
    } 

    $scope.sub = 0; 
    $scope.subFunc = function() { 
     $scope.sub=$scope.c-$scope.d; 
    } 
}); 
</script> 
<table style="width:100%" border=1> 
    <tr> 
     <th>Id</th> 
     <th>a</th> 
     <th>b</th> 
     <th>c</th> 
     <th>d</th> 
     <th>Addition</th> 
     <th>Subtraction</th> 
    </tr> 
<?php 
foreach($select as $slct) 
{?> 
<tr> 
    <td><?php echo $slct->id;?></td> 
    <td><?php echo $slct->a;?></td> 
    <td><?php echo $slct->b;?></td> 
    <td><?php echo $slct->c;?></td> 
    <td><?php echo $slct->d;?></td> 
    <td><?php echo $slct->addition;?></td> 
    <td><?php echo $slct->subtraction;?></td> 
    <br/> 
<?php } 
?> 
</body> 
</html> 

但一個新的問題在這裏提出,這個計算器沒有顯示在屏幕上加/減的結果。它的輸出顯示在屏幕上:

0

而且我也解決了這個問題... 現在數據已成功插入數據庫並顯示在屏幕上。一探究竟..! 這裏是視圖頁:

<!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/> 
Name: <input type="text" ng-model="nam" name="nam"><br/> 
Email: <input type="email" ng-model="email" name="email"><br/> 
Password: <input type="password" ng-model="password" name="password"><br/> 
City: <input type="text" ng-model="city" name="city"><br/> 
<button ng-click="addFunc()" type="submit">Sub</button> 

</div> 
</form> 
<script> 
var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope,$http) { 

    $scope.addFunc = function() { 
     $http({ 
      method : 'POST', 
      url  : 'http://localhost/crud/public/registration_data_page', 
      data : {nam:$scope.nam,email:$scope.email,password:$scope.password,city:$scope.city} 
     }).then(function successCallback(response) { 
      console.log('successCallback'); 
      console.log(response); 

    }, function errorCallback(response) { 
      console.log('errorCallback'); 
      console.log(response); 
    }); 

} 
}); 
</script> 



</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 calculative_function(Request $request) 
    { 
     echo $a_value = $request->a; 
     echo $b_value = $request->b; 
     echo $c_value = $request->c; 
     echo $d_value = $request->d; 

     if($a_value) 
     { 
     $calc = DB::table('calculation')->insert(['a' => $a_value, 'b' => $b_value, 'addition'=>$a_value+$b_value]); 
     } 
     else 
     { 
     $calc = DB::table('calculation')->insert(['c' => $c_value, 'd' => $d_value,'subtraction'=>$c_value-$d_value]); 
     } 
    } 
    public function homework_function(Request $request) 
    { 
     $select = DB::select('select * from calculation'); 
     return view('homework',['select' => $select]); 
    } 

和路由是與上述相同的答案。

相關問題