I am using laravel collective form but on submit it just reload and stays on same page.
It is not going to the controller method provided in action attribute.
Here is my form code
{!! Form::open(['action' => 'UsrController#store', 'method'=>'POST']) !!}
<div class="form-group">
{{Form::label('ph_no','Phone Number')}}
{{Form::text('ph_no','',['class'=>'form-control','placeholder'=>'+92 3342079421'])}}
<br>
{{Form::label('user_type_id','User Type')}}
<br>
{{Form::select('user_type_id',$user_array,null,['class'=>'form-control','placeholder'=>'Please Select a User Type']
)}}
<br>
{{Form::label('country_id','Country')}}
<br>
{{Form::select('country_id',$country_array,null,['class'=>'form-control','placeholder'=>'Please Select a Country']
)}}
</div>
{{Form::submit('Submit',['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
UsrController store method reference:
public function store(Request $request)
{
$this->validate($request,[
'name'=>'required',
'email'=>'required',
'user_type_id'=>'required'
]);
$use= new User();
$use->phno=$request->input('ph_no');
$use->user_type_id=$request->input('user_type_id');
$use->country_id=$request->input('country_id');
$use->save();
return redirect('/User')->with('success','User Created!');
}
I am using laravel collective v.6.0
Edit:
Route:
Route::resource('Usr','UsrController');
html generated for the form:
<form method="POST" action="http://localhost/final/hire/public/Usr" accept-charset="UTF-8">
<input name="_token" type="hidden" value="8QMxOxKHVs4LPy0SZ1NN6KgKevMRJwvPa4jC9lEj">
<div class="form-group">
<label for="ph_no">Phone Number</label>
<input placeholder="+92 3342079421" name="ph_no" type="text" value="" id="ph_no" class="form-control">
<br>
<label for="user_type_id">User Type</label>
<br>
<select id="user_type_id" name="user_type_id" class="form-control"><option selected="selected" value="">Please Select a User Type</option><option value="1">Poster</option><option value="2">Worker</option></select>
<br>
<label for="country_id">Country</label>
<br>
<select id="country_id" name="country_id" class="form-control"><option selected="selected" value="">Please Select a Country</option><option value="1">Pakistan</option></select>
</div>
<input type="submit" value="Submit" class="btn btn-primary">
</form>
Assuming that it wasn't a typo in the code you posted, I would suggest switching over to Route::resource('User','UserController'); and also updating the name of the controller to UserController.
In the store method you're redirecting to '/User' but that route wouldn't be created from the route snippet you posted.
Edit:
It looks like it's failing validation. name and email are required, but not present in the form.
Related
I have a form with two input fields, that should route to search.servicecity with the two input fields as parameter (service and city). How can I achieve to say, that by clicking the submit button, the input fields are used as the parameters?
{!! Form::open(['route' => ['search.servicecity',service,city]]) !!}
<div class="row">
<div class='col-md-5'>
<input type="text" class='form-control form-control-lg' name="service" id="service" placeholder="activity" data-action="{{ route('search.autocompleteservice') }}"/>
<div id='searchresultservice' style='text-align:left'></div>
</div>
<div class='col-md-5'>
<input type="text" class='form-control form-control-lg' name="city" id="city" placeholder="city or zip" data-action="{{ route('search.autocompletecity') }}"/>
<div id='searchresultcity' style='text-align:left'></div>
</div>
{{ Form::submit('Suchen', array('class'=>'btn btn-success btn-lg btn-block col-md-2'))}}
</div>
{!! Form::close() !!}
You should have code similar to below. You would obviously need to validate your inputs before trusting anything from the client. You could easily add validation in the view also. The routes file could be routes.php depending on your version of laravel.
File: routes\web.php
Route::get('search/servicecity', 'SearchController#index')->name('searchServiceCityForm');
Route::post('search/servicecity', 'SearchController#process')->name('processServiceCity');
File: app\Http\Controller\SearchController.php
class SearchController extends Controller
{
public function index()
{
return view('search.ServiceCity');
}
public function process(Request $request)
{
$service = $request->input('service');
$city = $request->input('city');
/* Do something with data */
return view(search.result, compact('service','city'));
}
File: resources\views\search\ServiceCity.blade.php
<html><head><title>Search for City and Service</title></head><body>
<form method="post" action="{{url('search/servicecity')}}">
{{csrf_field()}}
<div>
<label for="Service">Service:</label>
<input type="text" name="service">
</div>
<div>
<label for="city">City:</label>
<input type="text" name="city">
</div>
</body></html>
File: resources\views\search\Result.blade.php
<html><head><title>Result of Service City Search</title></head><body>
<div><span>Searched for service: {{ $service }}</span></div>
<div><span>Searched for city: {{ $city }}</span></div>
</body></html>
</html>
I have a controller TourCategoryController.php and has edit method:
public function edit(TCategory $tCategory)
{
return view('admin.manage.tour.category.edit')->withCategory($tCategory);
}
And below is the code from my view edit:
<div class="col-sm-4">
{{Form::model($category,['route' => ['tour-category.update', $category->id ], 'method' => "PUT"]) }}
<input type="text" class="form-control" id="name" name="name">
<label for="name">Name</label>
{{ Form::close() }}
</div>
The trouble I'm having is, the input field is not being filled with form modal binding.
While inspecting the edit form action attribute shows action="http://localhost:8000/manage/tour-category" while it should be like action="http://localhost:8000/manage/tour-category/{id}"
Route for the controller:
Route::prefix('manage')
->middleware('role:superadministrator|administrator|user')
->group(function () {
Route::resource('tour-category','TourCategoryController');
});
Use laravel text field instead of plain form text field.
{{ Form::text('name',null,['class'=>'form-control','id'=>'name']) }}
If you are not using Form Facades
<div class="col-sm-4">
<form method="POST" action="{{ route('tour-category.update', $category->id) }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<label for="name">Name</label>
<input type="text" id="name" name="name" class="form-control">
</form>
</div>
use
{{ Form::text('name',null,['class'=>'form-control','id'=>'name']) }}
instead of
<input type="text" class="form-control" id="name" name="name">
(SOLVED) Thanks..
I just want to make a new view called "tambah.blade.php" and the controller is "JurnalController.php" with method "tambahJurnal", but it show an error. What's wrong with my route?
Here is my form:
<h1>Tambah Jurnal</h1>
<form method="post" class="tambahJurnal" action="{{ route('tambah') }}" >
{{ csrf_field() }}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
</form>
And here is my method in JurnalController:
public function tambahJurnal(Request $request){
$jurnal = new Jurnals;
$jurnal->no_jurnal = $request->no_jurnal;
$jurnal->tgl_jurnal = $request->tgl_jurnal;
$jurnal->keterangan = $request->keterangan;
$jurnal->save();
}
This is my route:
Route::post('/tambah', 'JurnalController#tambahJurnal');
And it show an error like this:
enter image description here
You are creating a route for the POST method with this line:
Route::post('/tambah', 'JurnalController#tambahJurnal');
But then, you're trying to perform a GET request with your browser on that URL. That's why you're getting that error.
Try adding this line as well:
Route::get('/tambah', 'JurnalController#tambahJurnal');
#1. Add this route in your route file.
Route::get('/tambah', function()
{
return view('tambah');
});
#2. change in tambah.blade.php file
<form method="post" class="tambahJurnal" action="{{ route('tambah') }}" >
to
<form method="post" class="tambahJurnal" action="{{ url('tambah') }}" >
Thanks
You can write this. Hopefully this will solve your problem.
<h1>Tambah Jurnal</h1>
<form method="post" class="tambahJurnal" action="{{ url('tambah') }}" >
{{ csrf_field() }}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
</form>
try in form action ="/tambah"
try with
Route::any('/tambah', 'JurnalController#tambahJurnal');
first then if it works fine you can change to
Route::post('/tambah', 'JurnalController#tambahJurnal');
any will work for get post put ....
Change this line
Route::post('/tambah', 'JurnalController#tambahJurnal');
to
Route::post('tambah', 'JurnalController#tambahJurnal')->name('tambah');
and use blade Form
<h1>Tambah Jurnal</h1>
{!! Form::open(['route' => 'tambah','method' => 'POST','class' => 'tambahJurnal']) !!}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
{!! Form::close() !!}
Advantage of using blade Form is , you don't explicitly need to specify {{ csrf_field() }}, blade injects csrf token itself.
Add route to show view
Route::get('/tambah', 'JurnalController#index');
And add index method to your controller
public function index(){
return view("tambah");
}
Also add /
action="{{ route('/tambah') }}"
Sometimes it happened that you are in /tambah and trying to post url becomes /tambah/tambah
I will process the data from the form
then I click the add button and get an error
Whoops, looks like something went wrong.
TokenMismatchException in VerifyCsrfToken.php line 67:
i have view
<form action="{{ url('siswa') }}" method="post">
<div class="form-group">
<label for="nisn" class="control-label">NISN</label>
<input name="nisn" id="nisn" type="text" class="form-control">
</div>
<div class="form-group">
<label for="nama_siswa" class="control-label">Nama Siswa</label>
<input name="nama_siswa" id="nama_siswa" type="text" class="form-control">
</div>
<div class="form-group">
<label for="tanggal_lahir" class="control-label">Tanggal Lahir</label>
<input name="tanggal_lahir" id="tanggal_lahir" type="date" class="form-control">
</div>
<div class="form-group">
<label for="jenis_kelamin" class="control-label">Jenis Kelamin</label>
<div class="radio">
<label><input name="jenis_kelamin" type="radio" value="L" id="jenis_kelamin"> Laki-laki</label>
</div>
<div class="radio">
<label><input name="jenis_kelamin" type="radio" value="P" id="jenis_kelamin"> Perempuan</label>
</div>
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Tambah Siswa">
</div>
</form>
and then this is my controller
public function create()
{
return view('siswa.create');
}
public function store(Request $request)
{
$siswa = $request -> all();
return $siswa;
}
you need to add {{csrf_field()}} inside the form. it will create a csrf token, which is needed to submit a form
You need to add this {{ csrf_field() }} between your form tags.Read here for more information https://laravel.com/docs/5.4/csrf
There are many options to solve this problem.
1) You can take hidden input field for token inside your form like:
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
2) Add following code before the closing tag of your form:
{{ Form::token() }}
3) Or use laravel form syntax to avoid token mismatch problem like below.
{{ Form::open(array('url' => 'foo/bar')) }}
//
{{ Form::close() }}
4) Or in the html form structure you can also use csrf field like below.
<form method="POST" action="/profile">
{{ csrf_field() }}
...
</form>
5) Or lastly.
<form method="POST" action="/profile">
{!! csrf_field() !!}
...
</form>
This will definately work for you.
Thanks
I have a couple of search forms in my app in which pagination works great, but when it comes to date search I only get the first page, when I try to go to the second page I get an undefined $ticket variable. When I look at the URL I can see that it doesn't take the date values with him to the next page.
Here is my code:
<tbody class="searchTable">
#foreach($ticket as $tickets)
<tr>
<td>
{{Carbon::parse($tickets->created_at)->format('m/d/Y')}}
</td>
<td>{{$tickets->id}}</td>
<td>{{$tickets->clientName}}</td>
<td>
{{Carbon::parse($tickets->dueDate)->format('m/d/Y')}}
</td>
<td>{{$tickets->refNumber}}</td>
<td>{{$tickets->invoiceNumber}}</td>
<td>{{$tickets->jobLocation}}</td>
<td>{{$tickets->workDescription}}</td>
<td>{{$tickets->jobStatus}}</td>
</tr>
#endforeach
</tbody>
{!! $ticket->appends(Request::only('_token','search'))->render() !!}
This is the controller:
$ticket = DB::table('tickets')
->whereBetween('created_at', [$newDateFrom, $newDateTo])
->orderBy('created_at', 'desc')
->paginate(10);
return view('ticketsView', compact('ticket'));
Here is my solution:
I have the same view for different search requests so I append the results to the paginator so it will have it on the next pages as well. The name is the value of the name attribute in the search form. In my case I had multiple. Here is an example:
{!! $ticket->appends(Request::only(['dateFrom'=>'dateFrom', 'dateTo'=>'dateTo', 'search'=>'search', 'filter'=>'filter', 'dueDateFrom'=>'dueDateFrom','dueDateTo'=>'dueDateTo']))->render() !!}
So now if my search results will contain in the URL dateTo and dateFrom values for example then it will be saved to all pages. It's important to understand that these values come from the name attribute of your search form.
Here is an example:
<form class="form-horizontal" role="form" method="GET" action="/ticket/searchresults" accept-charset="UTF-8" enctype="multipart/form-data">
<div class="text-centered">
<p><strong>Search by dates:</strong></p>
</div>
<div class="form-group">
<label for="filter">Select Client (optional)</label>
<select class="form-control" name="filter" type="text">
<option disabled selected> -- select client -- </option>
#foreach($selectClient as $selectClients)
<option value="{{$selectClients->name}}">{{$selectClients->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="dateFrom">From:</label>
<input class="datepicker2 form-control" name="dateFrom" type="text" required/>
</div>
<div class="form-group">
<label for="dateTo">To:</label>
<input class="datepicker2 form-control" name="dateTo" type="text" required/>
</div>
<button type="submit" class="btn btn-primary"><span class="fa fa-fw fa-search"></span></button>
</form>