Search form in Laravel - php

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>

Related

laravel form not going to controller on submit

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.

How to fix edit interface getting only first word of a column in table in Laravel 5.7?

My edit interface edit.blade.php only gets the first word of the name from my database, this is what the index.blade.php looks like
and when i click on the edit icon of the 3rd line it leads me to edit.blade.php which gives me this
"Nom d'établissement" textfield only gets the first word from the database
Everything looks fine in the database:
this is my edit.blade.php form:
<form method="post" action="{{ route('etablissements.update', $etablissement->id) }}">
#method('PATCH')
#csrf
<div class="col-5">
<div class="form-group">
<label for="nom">Nom Etablissement :</label>
<input type="text" class="form-control" name="nom" value={{ $etablissement->nom }} />
</div>
<div class="form-group">
<label for="price">E-Mail :</label>
<input type="text" class="form-control" name="email" value={{ $etablissement->email }} />
</div>
<div class="form-group">
<label for="quantity">Telephone:</label>
<input type="text" class="form-control" name="telephone" value={{ $etablissement->telephone }} />
</div>
</div>
<button type="submit" class="btn btn-primary">Confirmer</button>
</form>
this is edit function in the controller:
public function edit($id)
{
$etablissement = Etablissement::find($id);
return view('etablissements.edit', compact('etablissement'));
}
and this is update function in the controller:
public function update(Request $request, $id)
{
$request->validate([
'nom'=>'required',
'email'=> 'required',
'telephone' => 'required|numeric'
]);
$etablissement = Etablissement::find($id);
$etablissement->nom = $request->get('nom');
$etablissement->email = $request->get('email');
$etablissement->telephone = $request->get('telephone');
$etablissement->save();
return redirect('/etablissements')->with('success', 'Utilisateur édité');
}
Quote the value attribute.
<input type="text" class="form-control" name="nom" value="{{ $etablissement->nom }}" />
Without quotes, the second word in$etablissement->nom is interpreted as another attribute rather than part of the value of the value attribute.
The email and telephone values are showing up correctly because there are no spaces, but you should quote those as well just in case.

laravel 5.3 MethodNotAllowedHttpException in RouteCollection.php line 218:

(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

Laravel 5.2 TokenMismatchException in VerifyCsrfToken.php line 67

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

Laravel form not submitting data to database

For some reason, my form isn't submitting any data to the database, but it looks fine from where I'm standing and the database can call the information to the form fine.
Since there's a lot of information for people to submit, I'm making the profile details not part of the login process. Still unfamiliar with how Laravel does these but I roughly get the process now that I've been fiddling.
One thing I'm wondering, is there a specific syntax for forms to write to the database, should I be naming the database's respective table names in the form? Or is that part of the Controller?
Should I be using Form Model Binding? It's a little hard to find information on that that's for the current version of Laravel though.
What am I missing?
//Route::get('NewUser', 'UserEntryController#create');
Route::post('NewUser', 'UserEntryController#UserForm');
//Route::get('NewUser', 'UserEntryController#create')->name('NewUser');
Route::post('NewUser', 'UserEntryController#UserForm')->name('submit');
Controller:
<?php
namespace App\Http\Controllers;
use App\UserEdit;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
class UserEntryController extends Controller
{
protected function create()
{
$id = UserEdit::find(715)->toArray();
return view('NewUser', compact('id'));
//return $array;
}
public function UserForm(Request $request) {
$email = $request['email'];
$first_name = $request['first_name'];
$password_hint = $request['password_hint'];
$last_name = $request['last_name'];
$user = UserEdit::find(715)->first();
$user->email = $email;
$user->First_Name = $first_name;
$user->Last_Name = $last_name;
$user->Password_Hint = $password_hint;
$user->save();
$id = UserEdit::find(715)->toArray();
return view('NewUser', compact('id'));
}
}
Blade:
#extends('layout')
#section('content')
<h1> Add Your Information {{ $id['name'] }}</h1>
<div class="row">
<div class="col-md-6">
<h3>Edit</h3>
<form action="{{ route('submit') }}" method="post">
<div class="form-group">
{{ csrf_field() }}
<label for="email">Your E-Mail</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
{{ csrf_field() }}
<label for="first_name">Your First Name</label>
<input class="form-control" type="text" name="first_name" id="first_name">
</div>
<div class="form-group">
{{ csrf_field() }}
<label for="last_name">Your Last Name</label>
<input class="form-control" type="text" name="last_name" id="last_name">
</div>
<div class="form-group">
{{ csrf_field() }}
<label for="password_hint">Your Password Hint</label>
<input class="form-control" type="text" name="password_hint" id="password_hint">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
#foreach ($id as $key=>$value)
{{ $value }}<br>
#endforeach
#stop
You should use $request->get('email') instead of $request['email'] and the same for everything else you want from the request, and I don't think you have to use ->first() when using ->find
There could be something wrong with the _token field, since there are five csrf fields in the form.
Try to remove this line
<input type="hidden" name="_token" value="{{ Session::token() }}">
and just leave one {{ csrf_field() }} in the form.

Categories