(Laravel) สอบถามเรื่องการส่งค่า ajax แบบ post หน่อยครับ
laravel 5.2 + laravelcollective/html
keywork: csrf
route
<?php
Route::group(['middleware' => ['web']], function () {
// Auth Route...
Route::group(['middleware' => 'auth'], function () {
// Ajax Route...
Route::post('/ajax/cabinet', [
'before' => 'csrf',
'uses' => 'AjaxController@postCabinet'
]);
});
});
view
<div class="box-body">
<div class="row">
<div class="col-md-6">
{!! Form::open(['url' => '/', 'role' => 'form']) !!}
<div class="row">
<div class="col-md-5">
<div class="form-group">
{!! Form::label('cabinet', 'คณะรัฐมนตรี') !!}
{!! Form::select('cabinet', $cabinets->lists('name', 'id'),
$cabinets->first(function($key, $value) { return $value['default'] == true; })->id,
['class' => 'form-control']) !!}
</div>
</div>
<div class="col-md-5">
<div class="form-group">
{!! Form::label('budget_year', 'ปีงบประมาณ') !!}
{!! Form::select('budget_year', $budget_years->lists('name', 'id'),
$budget_years->max('budget_year_id'),
['class' => 'form-control']) !!}
</div>
</div>
<div class="col-md-2 bottom-align-text">
<div class="form-group">
{!! Form::submit('ตกลง', ['class' => 'form-control btn btn-primary']) !!}
</div>
</div>
</div>
{!! Form::close() !!}
</div>
<!-- /.col -->
<div class="col-md-6">
<div class="pad">
<!-- Map will be created here -->
<div id="world-map-markers" style="height: 325px;"></div>
</div>
</div>
<!-- /.col -->
</div>
<!-- /.row -->
script
$('#cabinet').change(function () {
$.ajax({
url: 'ajax/cabinet',
type: "post",
data: {
'cabinet': $('#cabinet').find(":selected").val(),
'_token': $("input[name='_token']").val()
},
success: function (budgetYears) {
var options = '';
$.each(budgetYears, function (i, budgetYear) {
options += '<option value="' + budgetYear.id + '"' + ((budgetYears.length == i + 1) ? ' selected="selected"' : '') + '>' + budgetYear.name + '</option>'
});
$('#budget_year').html(options);
}
});
});
controller
<?php
namespace App\Http\Controllers\Webapp;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Input;
use App\Cabinet;
class AjaxController extends Controller
{
public function postCabinet() {
$cabinet_id = Input::get('cabinet');
$budget_years = Cabinet::where('id', $cabinet_id)
->first()
->budget_years()
->orderBy('id', 'asc')
->get();
return $budget_years;
}
}
Date :
2016-03-17 16:12:28
By :
ห้ามตอบเกินวันละ 2 กระทู้
ขอบคุณครับ
Date :
2016-03-17 17:03:33
By :
Pro1
Date :
2016-03-18 09:57:54
By :
mr.win
Laravel
{{$cont}}
สอบถามหน่อย ผมจะเอาตัวแปร $cont ที่ส่งมาเอาไปแทนที่ content ได้ยังไงครับ
@include('components.content')
Date :
2018-03-30 12:31:59
By :
arnon
Load balance : Server 02