我正在尝试使用Laravel4中的ANGULARJS将数据插入到数据库中。但数据不会进入DataBase。这里没有错误显示。在这里,我还在php中包含了foreach循环以显示数据。
这是视图页面(* .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', 'NewController@homework_function');
Route::get('/calculative_page', 'NewController@calculative_function');
答案 0 :(得分:0)
我自己解决了,Route.php
或Controller
没有变化。唯一的变化是在视图页面中。请查看以下代码:
<!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>
但是这里出现了一个新问题,这个计算器没有在屏幕上显示加法/减法结果。其输出在屏幕上显示为:
答案 1 :(得分: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]);
}
,路线与上述答案相同。