Hidden inputs in Laravel blade.php - php

Hey so I'm trying to sort entries by a type of pet, the code below is code from my blade.php
<div>
<td>
<form>
#csrf
<input name="cat" type="hidden" value="cat">
<a name="cat" href="{{ url('sorting') }}" value="cat">Cat</a>
</form>
</td>
</div>
In the blade file I'd have multiple links such as cat, dog, rabbit which essentially act as filtering options
I have a sort method in my controller that does the following
public function sorting(Request $request){
if($request->input('cat') === 'cat'){
$pets = Pet::Where('type', 'cat')->get();
return view('index', compact('pets'));
}
}
In my sort method, I'm trying to check if the cat link is clicked and then if it is it would return only pets of type cat, the problem I have is that my $request->input('cat') is returning a null. How would I correct this?

You have multiple issues in your code:
You don't seem to have a way to actually submit the form. The link in the post won't do it on it's own (unless you have some event on that link in JS)
<a>-tags don't have a value-attribute and the name-attribute means something completely different for links and is not for submitting data through forms.
A form without a method will use GET as default. You're trying to retrieve the value in PHP using $request->input() which is for POST-requests. For GET requests (which uses the query string to pass data), use $request->query().
However... you don't need the form. Just pass the value as a query parameter in the link instead:
<td>
Cat
</td>
Then in your PHP code, retrieve the value using:
if ($request->query('sort') === 'cat') {
// your code
}

Related

Basic form in Wordpress template

I need to simply get a search term from a form into a variable. I have set up a basic form within a template file, that template file is then linked to a page within Wordpress admin. The problem I am getting is that the form doesn't submit so I am unable to use the variable. If I remove get_header(); from the template then the form will submit but obviously it break wordpress stuff.
Here is my form:
<form action="<?php the_permalink(); ?>" method="post" autocomplete="off" >
<label>
<input placeholder="Search…" name="qcsearch" type="text">
</label>
<input type="submit" name="submit" value="Submit">
</ul>
</form>
I have tried leaving out the action, using the template name which is qccerts.php and using $_SERVER['PHP_SELF']
Here is then what I am trying to do with the output:
if(isset($_POST['submit'])){
$searchterm = $_POST["qcsearch"];
}else{
$searchterm = '';
}
Its basically a simple search which tells the users if there is a file by the name they search. So I need to populate $searchterm so I can use it later down the page.
Any help appreciated.
It's difficult to determine what your exact problem is without a reproducible scenario. For example, without seeing your problem, I'm not sure whether the form is really not submitted at all, or submitted, but you did not see it being executed, or there is some Javascript which prevents your form from submitting. There is a possibility that the form is submitted to the wrong action as well.
However, if you intend to keep your search term accross the pages, you could add it into session. Let's imagine these functions:
function storeSearchTerm($searchTerm) {
$_SESSION["searchterm"] = $searchTerm;
}
function getSearchTerm() {
return isset($_SESSION["searchterm"]) ? $_SESSION["searchterm"] : "";
}
By calling these functions you can manage the search term, initializing it via storeSearchTerm($_POST["qcsearch"]) or something.
As about your actual form, if it does not work, then you can submit the form in Javascript, such as
document.getElementById("myForm").submit();
and make sure that this is triggered either via an onclick attribute, or a click event listener on the button created via addEventListener.
EDIT
It turns out that a class name was not well formed (case-sensitivity issue).

Trying to get value of checkbox from form to controller

I'm trying to do something that allows me to upload photos when I check a checkbox control. I got the client side working correctly such that when the checkbox is checked the upload controls are displayed and the form validates correctly.
However, in my controller I need to take some action if my checkbox was checked (true) calling a certain method. If it isn't checked (false) I perform some other action.
In my html page I have the following:
<form action="/supplier/submit/plan" method="post" role="form" id="plan-form">
...
<input name="checkingPhotos" type="checkbox" id="chkPhotos" />
<label for="chkPhotos">I want to include photos in this plan.</label>
...
</form>
However, in my controller I just want to for now see if I get the correct value in my checkbox. For this I did something simple as:
public function submitPlan(Request $request)
{
$checkboxValue = $request->input('checkingPhotos');
dd($checkboxValue);
}
The result is null is printed whether I check the checkbox or not. My route also looks like this:
Route::post('/submit/plan', 'SupplierController#submitPlan');
Can someone please tell me what I am doing wrong here? I just want to see the value 1 / True or 0 / False in my controller method.
Value
The real issue is that you don't have a value for your check box. Add a value, and your problem is solved. It should be:
<input name="checkingPhotos" type="checkbox" id="chkPhotos" value="1" />
[] creates an array
The answer Dylan Kas submitted about changeing the name to add [] works, but not for the reasons you think. Lets take a look:
<input name="checkingPhotos[]" type="checkbox" id="chkPhotos" />
Will pass in the post string:
checkingPhotos[]=On
Which, in PHP will automatically be turned into an array.
Refernce Frame Challenege
Why do you have a checkbox in the first place? Is the checkbox needed? Why not just check for the existence of the file.
$validated = $request->validate([
//... other fields here.
'image' => 'mime:png,gif,jpeg|max:10000' //set max to file size limit you want
]);
$plan = new SupplierPlan($validated);
if($request->has('image')){
$plan->image = $request->image->store();
}
$plan->save();
// ... flash message, return view or redirect
If it's a checkbox you should modify the name of your input to
<input name="checkingPhotos[]" type="checkbox" id="chkPhotos" />
Then you should be able to get the value as you want it with
$request->input('checkingPhotos');
It seems your problem is in your route, change it to
Route::post('/supplier/submit/plan', 'SupplierController#submitPlan');

Laravel 5 : Why is my request->input returning null

I'm trying to return the value of a checkbox within my laravel controller, but every time I request a input from a checkbox element in a form, it returns null.
My controller, retrieving the input of a element called Filter-Method.
Here I'm trying to request a input method called filter-method which is a checkbox.
My Route, since this function will execute on a button:
My Blade, where I'm trying to retrieve the result of my filter-method checkbox:
On line 38 I have a checkbox called filter-method, and when you click on the button on line 115 it should send a request to the controller where it would return a result but instead it returns null
Any ideas of why I'm returning null?
You are not passing any parameter named filter-method. If you are posting values you should use post method.
Like following
Route::post('GetFilterByColumns','MentorController#FilterByValuesColoumns')
If you want to list data according to filter-method then try the following.
Route::get('GetFilterByColumns/{filter-method}','MentorController#FilterByValuesColoumns')
And in your mentorlist.blade.php page
change the href value according to route.
You have to add form to your blade around checkbox either with get or post method as per your requirement change route according to your form method
consider demo blade file
<form method="get" action="{{ url('GetFilterByColumns') }}" class="form-horizontal form-label-left" id="">
<div class="checkbox">
<label><input type="checkbox" name="filter-method" value="filter-method>Method</label>
</div>
<input type="submit" value="submit"/>
</form>
Now you will get a checkbox value in contoller
This is very basic thing just use form instead of <a> tag. Every time you need to send input value to server you need to use <form> element. In image you have uploaded there is no <form> and you are using a <a> tag.
You need to do it like this
<form method="get" action="/getFilterBycolumns">
<input type="checkbox" name="filter-method">
// other input fields
// and then a submit button instead of <a>
<button class="your-class" type="submit"> Send</button>
just use POST instead of GET if you are sending form values

Laravel giving a html form a unique url

In my blade I have field called $jobs. I am trying give every single form a unique url based on the $job->id. So the url is jobs/$job->id. However when I click on the a tag for to submit the form all the urls show jobs/the last $job->id. In my case all the urls show job/386. What should I so every url has a unique url? Here is my code.
#foreach($jobs as $job)
<form method="post" action="{{url('jobs/'. $job->id)}}" id="start-jobs">
{{csrf_field()}}
<a onclick="document.getElementById('start-jobs').submit()">( start )</a>
</form>
#endforeach
This is actually a JavaScript problem, not a Laravel problem.
All your forms have the same id (which isn't actually valid HTML), and which is why getElementById('start-jobs') is getting you the last one. If you need to have an identifier for all of the forms, use class instead of id.
It seems like you should be able to use just a regular submit button instead of the submit link you're using.
#foreach($jobs as $job)
<form method="post" action="{{url('jobs/'. $job->id)}}" class="start-jobs">
{{csrf_field()}}
<input type="submit" value="( start )">
</form>
#endforeach

Laravel pass variable from FirstController to SecondController

This is my two routes which pass variable on each view.
Route::resource('product', 'ProductController');
Route::resource('booking', 'BookingController');
I created a view which display the current product (ex. http://localhost:8000/product/15) using the ProductController.
Now i created another view to book this product then insert it (modal pop up) inside the product view using #include('partials.booking').
The problem is how can i pass the product id to the BookingController? It is possible?
This depends a lot on what framework you use in your frontend ( jquery etc. )
Usually your popup should somehow know what product it is referred to. You can do this for example like this
<!-- this is your html / blade -->
<div class="booking-popup">
<button class="book" data-product="{{$product->id}}">Book Now!</button>
</div>
You can then use ajax and jquery for example to get the data attribute
Another option is to provide a form with a hidden field
<form action="/booking" method="post">
{{csrf_field()}}
<input type="hidden" name="product_id" value="{{$product->id}}" />
<input type="submit" value="Book Now!" />
</form>
If you use the laravel html collective you can simplify it even more
EDIT
Since your Bookings actually belong to a product you could also rearrange your resources so that a booking is create with the route
/product/5/booking (POST)
You could then simply access the product id as parameter of the route
EDIT 2
To access it from your controller simply ( case of hidde form input )
public function store(\Illuminate\Http\Request $request) {
dd($request->input('product_id');
}
If you defined it via route simply go for whatever you set as placeholder
Route::post('product/{product}/book', 'BookingController#store');
Controller then:
public function store(\Illuminate\Http\Request $request, $product) {
dd($product)
}
Include your model page as #include('partials.booking', ['product'=>$product])
Change your form action to
<form action="{{url('product/'.$product->id.'/booking')}}" method="post"> and most important add _token hidden field if your CSRF middleware is enabled.
Change your route to
Route::post('product/{product_id}/booking', 'BookingController#store');

Categories