Reputation: 714
i am a very new comer in laravel framework version 5.1.10. Recently i am trying to make a simple project for CRUD in database. i am trying to send data from view to controller. but i am getting this error. i tried from stack and laravel.io/forum to find my solution but failed. that's why i need your suggestion. my routes.php code is below
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/index','all_control@index');
Route::post('/insert_students','all_control@insert_students');
Route::get('/form',array(
'as' => 'getform',
'uses' => 'all_control@getform'
));
Route::post('/form',array(
'as' => 'postform',
'uses' => 'all_control@postform'
));
and my form.blade.php is given below
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
<meta charset="UTF-8">
<meta name=description content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="csrf-token" content="{{ csrf_token() }}">
<!-- Bootstrap CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
<div class="container">
<h1>Form</h1>
<h3>
<?php
if(Session::has('message'))
{
echo Session::get('message');
}
?>
</h3>
<form action="{{ URL::route('postform') }}" method="post" role="form">
<legend>Form Title</legend>
<div class="form-group">
<label for="">Roll</label>
<input type="text" class="form-control" name="roll" id="roll" placeholder="Input...">
</div>
<div class="form-group">
<label for="">Marks of Physisc CT</label>
<input type="number" class="form-control" name="ct" id="ct" placeholder="Input...">
</div>
<button type="submit" class="btn btn-primary">Hit the Button</button>
</form>
</div>
</body>
</html>
and my all_control.php is here
<?php
namespace App\Http\Controllers;
use App\Model\students;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class all_control extends Controller
{
public function getform()
{
return view('form');
}
public function postform()
{
$roll = Input::get('roll');
$ct_number = Input::get('ct');
Session::flash('message','You have done successfully!!!!');
return Redirect::route('getform');
}
}
Upvotes: 0
Views: 557
Reputation: 464
I had also encountered this error last week but when i am switching to ajax post now it's working fine i don't know in php post but you can try:
//must be in the head tag
<meta name="csrf-token" content="{{ csrf_token() }}">
<script type=text/javascript>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
</script>
Upvotes: 0
Reputation: 1643
just add this
<input type="hidden" name="_token" value="{{ csrf_token(); }}">
right after opening form
<form action="{{ URL::route('postform') }}" method="post" role="form">
<input type="hidden" name="_token" value="{{ csrf_token(); }}">
<legend>Form Title</legend>
<div class="form-group">
<label for="">Roll</label>
<input type="text" class="form-control" name="roll" id="roll" placeholder="Input...">
</div>
<div class="form-group">
<label for="">Marks of Physisc CT</label>
<input type="number" class="form-control" name="ct" id="ct" placeholder="Input...">
</div>
<button type="submit" class="btn btn-primary">Hit the Button</button>
</form>
Upvotes: 2