I am using Laravel 8. I have the following search box in my blade file:
<form name="searchForm" class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" method="POST" action="{{ url('search') }}">
#csrf
<input name="searchField" type="search" class="form-control form-control-dark" style="width: 426px; " placeholder="Search for news, symbols, tickers or companies">
</form>
I route this to my SearchController:
public function showSearchPage(Request $request) {
$field = $request->searchField;
$name = Input::get('searchField');
.
.
.
However, $request->searchField is null. My $request object looks like the following:
Besides the token, my input field is not within the request.
Any suggestions on what I am doing wrong?
I appreciate your replies!
Change the form attribute of your form to id and try the form attribute on the input field.
Normally you shouldn't have to define either of those, you might try removing the id from your form and also the form from the input field.
It is possible that the initial form attribute on your form caused some trouble.
Let me know if the second approach also worked.
What I know is that, use form id when you need to perform a validation for a script or you have multiple forms in a single page, try changing your form attribute to id, or better if you completely remove it
for search form its better to use GET method instead of POST
and instead of
type="search"
used
type="text" (for test)
added id="searchField" to your input and changed type to text form search. should work
<form name="searchForm" class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" method="POST" action="{{ url('search') }}">
#csrf
<input name="searchField" id="searchField" type="text" class="form-control form-control-dark" style="width: 426px; " placeholder="Search for news, symbols, tickers or companies">
</form>
Related
I'm trying to pass a predefined variable with a value into the #if part of the statement for comparison. This is for Laravel v8.0 on the blade.php. It only works if I hardcoded the value in it but not through the variable $value. I would need to have a placeholder variable to pass the value for comparison, as it will be inputted from user. Hence may I know what is the proper way to declare the variable as in this case? Sorry I'm just starting learning Laravel. Thanks for your help.
Eg:
$value = "abc123";
#foreach($surveys as $survey)
#if($survey->Unit_code == {{$value}})
<form action="" method="">
#csrf
<div class="form-group">
<label for="name">Name</label> <br>
<input type="text" name="Name" class="form-control" value="{{$survey->name}}">
</div>
<input type="submit" value="Save">
</form>
#endif
#endforeach
Just so this is in an answer form for other users that encounter this problem as a developer just learning laravel; the issue here is there was an output to the dom using {{}} where the blade directives (things that start with #) use the PHP vars as is an example of this would be as follows:
#if($survey->Unit_code == $value)
the other issue he was having, in this case, is assigning raw PHP on the blade file, this works the same way. Although, to add raw PHP to the dom you would use the #php directive
#php
$value = "abc123";
#endphp
#foreach($surveys as $survey)
#if($survey->Unit_code == $value)
<form action="" method="">
#csrf
<div class="form-group">
<label for="name">Name</label> <br>
<input type="text" name="Name" class="form-control" value="{{$survey->name}}">
</div>
<input type="submit" value="Save">
</form>
#endif
#endforeach
if the data of $value needed to be completely dynamic based on the backend then I would recommend passing it to the blade file via the view() function instead and assigning the value there or as nullable on the first pass of the function. Hope that helps!
You dont have to use {{ }} in #if condition .
When you use #if( ) everthing inside of it can translate into php .
So , you instead of using :
#if($survey->Unit_code == {{$value}})
Use :
#if($survey->Unit_code == $value )
Hope that works .
In my Laravel app, I am creating a dynamic search functionality using Eloquent and I have a customer search form which looks like this:
customers.search.blade.php Contrived Example
<form method="post" action="{{ route('customers.search') }}">
#csrf
<div class="form-group">
<label for="first_name" class="font-weight-bold">First Name</label>
<div class="input-group">
<div class="input-group-prepend">
<select name="first_name['operator']" class="custom-select">
<option value="%">%</option>
<option value="=">=</option>
<option value="!=">!=</option>
</select>
</div>
<input id="first_name" name="first_name['query']" type="text" class="form-control">
</div>
</div>
</form>
I have a SearchCustomerRequest (FormRequest) class which builds out the validation rules dynamically. I've dumped it, so you can see what the generated rules looks like:
In my CustomersController under search method, I did the following to see what the validated request array looks like:
class CustomersController extends Controller
{
// ...
public function search(SearchCustomerRequest $searchCustomerRequest)
{
dd($searchCustomerRequest->validated());
}
// ...
}
In order to test this, in the search form, I've selected the firstname['operator'] to % and typed the first_name['query'] to test and submitted the form.
I got the following response (i.e. empty array):
[]
So, I dumped the whole request object $searchCustomerRequest to see what was in the parameter bag and this is what I see:
As you can see, my request is valid and my rules also looks correct, yet the validation doesn't seem to be working as expected.
Any ideas what might be wrong here?
In your ParameterBag, the first_name properties operator and query are enclosed in single quotes. In your HTML, the name attribute should exclude the single quotes. Ex: name="first_name[query]"
How can I secure data from being change if user used inspect element in chrome to change id, price..etc, I know I can't prevent users from using inspect element and do changes but I dont want these changes to have effect
I used this in blade to pass data from button using ajax
<button id="Item_root" data-id="{{$product->product_id}}" data-detailsfield="{{$product->product_details}}" data-titlefield="{{$product->product_title}}" data-pricefield="{{$product->product_price}}" data-photofield="{{ asset('images/' . $product->product_image) }}" class="Item_root Button_root">
and from inspect element user can see it like this:
<button id="Item_root" data-id="19" data-detailsfield="Serves 6-8 People" data-titlefield="Package # 8U" data-pricefield="105.99" data-photofield="http://localhost/crisp/public/images/Chicken-Fajitas.jpg" class="Item_root Button_root">
<div class="Item_image" style="background-image:url('http://localhost/crisp/public/images/Chicken-Fajitas.jpg');"></div>
<div class="Item_itemContent">
<div class="Item_topSection">
<span class="Item_name styles_just-right styles_base styles_spacing-base">Package # 8U</span>
<span class="Item_price styles_just-right styles_base styles_spacing-base styles_semibold">$105.99</span>
</div>
<div class="Item_description styles_small styles_base styles_spacing-base styles_line-default">Serves 6-8 People</div>
</div>
</button>
To preventing change values by users in inspect element and send it again to controller, you should use CSRF_TOKEN in input fields,
Take a look at here:
In laravel 5.6
<form method="POST" action="/profile">
#csrf
...
</form>
And also Laravel has Validation for check all input type that you defined.
Here is the documerntation :
Laravel Validation
I'm trying to update my database using a form on my
edit.blade.php page as shown below. The edit part works correctly as the fields are filled in in the form as expected, however when i try to save, an error message of
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
is displayed. I have tried so many ways on how to fix it and I'm not sure where I'm going wrong. Hopefully it's something simple to fix?
edit.blade.php
#extends('layouts.app')
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<form method="post" action="{{ action('PostsController#update', $id) }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH" />
<h1>Edit Item</h1>
<div class="form-group">
<label for="item">Item:</label>
<input type="text" id="item" name="item" value="{{$post->item}}" class="form-control" required>
</div>
<div class="form-group">
<label for="weight">Weight (g):</label>
<input type="number" id="weight" value="{{$post->weight}}" name="weight" class="form-control">
</div>
<div class="form-group">
<label for="noofservings">No of Servings:</label>
<input type="number" id="noofservings" value="{{$post->noofservings}}" name="noofservings" class="form-control">
</div>
<div class="form-group">
<label for="calories">Calories (kcal):</label>
<input type="number" id="calories" name="calories" value="{{$post->calories}}" class="form-control">
</div>
<div class="form-group">
<label for="fat">Fat (g):</label>
<input type="number" id="fat" name="fat" value="{{$post->fat}}" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>
</div>
</div>
#endsection
PostsController.php
<?php
public function update(Request $request, $id)
{
$this->validate('$request', [
'item' => 'required'
]);
$post = Post::find($id);
$post->item = $request->input('item');
$post->weight = $request->input('weight');
$post->noofservings = $request->input('noofservings');
$post->calories = $request->input('calories');
$post->fat = $request->input('fat');
$post->save();
return redirect('/foodlog');
}
web.php
<?php
Route::get('edit/{id}', 'PostsController#edit');
Route::put('/edit', 'PostsController#update');
Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'id',
'user_id',
'item',
'weight',
'noofservings',
'calories',
'fat',
'created_at'
];
}
My website is a food log application and this function is so that they can edit their log.
Any help is greatly appreciated!
Based on Michael Czechowski I edited my answer to make this answer better, The main problem is inside your routes:
Route::put('/edit/{id}', 'PostsController#update');
You have to add the id inside your route parameters either. Your update() function needs two parameters, first the form parameters from the formular and second the $id of the edited log entry.
The second problem is , the form method field is 'patch' and your route method is 'put'.
The difference between 'patch' and 'put' is:
put: gets the data and update the row and makes a new row in the database from the data that you want to update.
patch: just updates the row and it does not make a new row.
so if you want to just update the old row change the route method to patch.
or if you really want to put the data, just change the put method field in your form.
simply by : {{method_field('PUT')}}
Remember, the form's and the route's methods must be same. If the form's method is put, the route method must be put; and vice-versa.
The main problem is inside your routes:
Route::put('/edit/{id}', 'PostsController#update');
You have to add the id inside your route parameters either. Your update() function needs two parameters, first the form parameters from the formular and second the $id of the edited log entry.
The second one is inside your HTML template:
<input type="hidden" name="_method" value="PUT" />
To hit the right route you have to add the corresponding method to your route Route::put('/edit/{id}', 'PostsController#update');.
A possible last problem
<form method="post" action="{{ action('PostsController#update', $post->id) }}">
I am not sure how your template works, but $id is possible not set inside your template. Maybe try to specify the ID depending on your post. Just to make it sure the ID comes from the shown post.
Further suggestions
Best practice is to use the symfony built-in FormBuilder. This would make it easier to target those special requests like PUT, PATCH, OPTIONS, DELETE etc.
I have a form like this:
<form class="form-horizontal" role="form" method="POST" name="myform" id="myform" action="{{ url('/mypage/') }}">
{{ csrf_field() }}
<div id="somecontent">
</div>
<div id="abutton">
</div>
</form>
Then some Jquery like this:
$('#somecontent').html('<select class="form-control" id="myselect" form="my"><option value="1">Hello</option><option value="2">World</option></select>');
And then I add a button like this:
button = $('<button type="submit" form="myform" class="btn btn-theme">Send</button>');
$('#abutton').html(button);
And I also change the action dynamically:
$("#myform").attr("action","/mypage/" + item_id);
Then I got this in the web file:
Route::post('/mypage/{item_id}','mycontroller#do_something');
And then do this in the controller:
public function do_something($item_id,Request $request){
dd($request);
}
But the $request is empty, it does not contain the value selected in the dynamically generated select.
Any ideas why?
you haven't added name attribute of select, add name attribute and see it will populate into $request.
Try below code.
$('#somecontent').html('<select name="select_name" class="form-control" id="myselect" form="my"><option value="1">Hello</option><option value="2">World</option></select>');