I have a landing page with Create, Edit and Delete buttons, The Edit button is linked to a select (dropdown) menu and my route for the URL is /events/{number}/edit.
How will I get my URL to write to this format as the page will already be loaded and then the selection is made.
<div class="col-sm-4">
<form method="get" action="{{ route('events.edit', ['event_id' => '2']) }}">
<label>Event Name</label>
<select name="event_edit" class="form-control form-control-lg">
#foreach($events as $event)
<option>{{$event->event_name}}</option>
#endforeach
</select>
<button type="submit" class="btn btn-primary btn-round">Edit</button>
</form>
</div>
The above works but I have to put the event_id manually which is set to 2, will javascript be needed for this one?
Route::resource('events', 'EventController');
GET|HEAD | events/{event}/edit | events.edit | App\Http\Controllers\EventController#edit | web
I do not have a larvel instance I can test on but simply put PHP is static so once a DOM is render you cannot update the output with PHP ie putting the $event->id in the action when a dropdown (or any element) is changed. If you wanted to you would need JavaScript. However, I would opt for a more straight forward approach.
You will need to create a generic edit route on your controller and put the id on the option as the value.
<div class="col-sm-4">
<form method="get" action="{{ route('events.edit') }}">
<label>Event Name</label>
<select name="event_edit" class="form-control form-control-lg">
#foreach($events as $event)
<option value="{{$event->id}}">{{$event->name}}</option>
#endforeach
</select>
</form>
</div>
Now when the form is submitted you will have the id of the event the user wanted to edit. Simply use that to get the event in the controller and render the edit screen for that event. If you want the url to be displayed nicely as site.com/edit/event/3 then you could use something like
return redirect()->route('route.name', [$param]);
which should redirect to that routes url with the param being the id then do all the logic for getting and displaying there.
hm. Do we need the form-action here? How about this:
<div class="col-sm-4">
<form method="get" action="">
<label>Event Name</label>
<select name="event_edit" class="form-control form-control-lg"
onChange="window.location.href='/events/' + this.options[this.selectedIndex].value + '/edit'">
#foreach($events as $event)
<option value="{{$event->id}}">{{$event->name}}</option>
#endforeach
</select>
</form>
</div>
Related
So I know how to retrieve data to views(using classes in controller)
but how can I return some data to layout which doesnt(or rather i dont know where they are) have its classes where i could get code to do so.
here is my form in which i would like to put cities name.
I always do it by giving to my frontend controller data from db using
$events = DB::select('select * from events');
return view('frontend/index', ['event' => $events]);
Edit
How to pass $city->id from loop to action?
My app.blade form:
<form method="POST" action="{!! route('cityView', ['id' => $city->id]) !!}" class="form-inline">
<div class="form-group">
<select name="city_id" class="form-control">
<option>xd</option>
#foreach(\App\Models\City::all() as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-info">Search</button></a>
</form>
But how can I do it for a layout?
Would making a layout controller help with it? Or maybe make another class inside existing controller and return view of layout view?
Second solution would be adding this form to every single view since there will be only 3 of them but i prefer solution with app.blade
You can call data directly from layout something like this:
<form method="POST" action="#" class="form-inline">
<div class="form-group">
<select name="room_size" class="form-control">
<option></option>
#foreach(\App\Models\City::all() as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-info">Search</button>
</form>
I am trying to create a form submit but I bumped to a unexpected scenario.
When form submitted by clicking the submit button
The web app reroute to my index page of the resource route
With the form variable and values appended in the URL.
Example
URL before submitting: http://127.0.0.1/admin/products/create
URL after submitted: http://127.0.0.1/admin/products?_token=qQ4klvK2egdsP77iMY4RQhXd5laJDUONRyuh8oQd&productTitle=&productPrice=
View (create.blade.php)
<form type="POST" name="productAddForm" action="{{ route('products.store') }}" >
#csrf
<div class="mb-3 col-5">
<label for="productTitle" class="form-label">Title</label>
<input name="productTitle" type="text" class="form-control" id="productTitle">
</div>
<div class="mb-3 col-5">
<label for="productPrice" class="form-label">Price</label>
<input name="productPrice" type="number" class="form-control" id="productPrice">
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
Controller (ProductController.php)
public function create()
{
return view('layouts/admin/product.create');
}
public function store(Request $request)
{
dd($request->all());
}
Route (web.php)
Route::resource('admin/products', ProductController::class)->middleware('auth');
The URL parameters in your second link are a giveaway that your form data is being serialized as a GET request instead of submitted as a POST request.
There is no <form> type= attribute. You need to use method=.
<form method="POST" ...
I am trying to submit a form using jQuery and ended up hanging the page. I have this one page which have listing and have used table for that. For each listing i am giving edit option. So when user click on edit button on the row, a form will drop below the tr tag and update button. When i click on the update button, page start loading but it keeps loading forever.
This is my form (Sample), I have foreach for listing, so for each row, we have a new form separated with id.
<form action="" class="edit_form 1" method="POST">
<div class="row filter-row">
<div class="col-sm-12 col-md-6">
<div class="form-group">
<label class="focus-label">Order Id</label>
<input type="text" name="order_id" class="form-control floating order_id" value="">
<div class="orderid_error text-danger form_error"></div>
</div>
</div>
</div>
<div class="text-center">
<button class="btn btn-primary submit-btn test_update" data-test_id="1" name="testupdate" type="button" value="test_update">Update</button>
</div>
</form>
JS
$(document).on("click",".test_update",function(){
var test_id = $(this).attr("data-test_id");
if(typeof test_id!="undefined" && test_id!=""){
$(".edit_form."+test_id).submit();
}
});
I think the issue is over here:
$(".edit_form."+test_id).submit();
jQuery searches for the element with class value as => 'edit_form.1' which does not exist in the code.
For this to work you need to have a unique id for every form(assuming you've already done it as you mentioned).
Try this:
$(document).on("click",".test_update",function(){
var test_id = $(this).attr("data-test_id");
if(typeof test_id!="undefined" && test_id!=""){
$(this).parent().parent().submit();
}
});
Since in the DOM nodes every button will be differently saved even though it has same class it shouldn't be a problem adding parent() as it will call the parent of the clicked element but not all the buttons with the class name.
Let me know if it worked. :)
Assigning multiple forms on a single page might be occurring the issue. Since you are using jQuery to submit the form, there is a workaround for the same.
Do not create your form inside the foreach, instead just create one form with empty input box and assign the value through jQuery and then submit the form.
<form id="example" action="" class="edit_form 1" method="POST">
<div class="row filter-row">
<div class="col-sm-12 col-md-6">
<div class="form-group">
<label class="focus-label">Order Id</label>
<input id="orderid" type="text" name="order_id" class="form-control floating order_id" value="">
<div class="orderid_error text-danger form_error"></div>
</div>
</div>
</div>
<div class="text-center">
<button class="btn btn-primary submit-btn test_update" data-test_id="1" name="testupdate" type="button" value="test_update">Update</button>
</div>
JS
$(document).on("click",".test_update",function(){
var test_id = $(this).attr("data-test_id");
if(typeof test_id!="undefined" && test_id!=""){
$("#orderid").val("YOUR VALUE");
$("#example").submit();
}
});
This will definitely work.
I'm trying to create a form that passes data via get to the controller but the URL looks allways like this:
http://example.com/test?_token=VinwWFxKIhKvMqrrEBN5xwXhrmYQjLnOWV8s7dht¶m1=horse¶m2=cat¶m3=dog
But I want something like this:
http://example.com/test/param1=horse/param2=cat/param3=dog
or
http://example.com/test/horse/cat/dog
Route:
Route::get('test/{param1}/{param2}/{param3}', ['as' => 'test', 'uses' => 'MainController#test']);
HTML:
<form action="{{ route('test') }}" method="get">
{{ csrf_field() }}
<div class="col-md-3">
<div class="form-group">
<label for="animal1">animal1</label>
<br>
<input type="text" name="animal1" class="form-control">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="animal2">animal2</label>
<input type="text" name="animal2" class="form-control">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="animal3>animal3</label>
<input type="text" name="animal3" class="form-control">
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
The problem is that the test route is reloaded every 10 seconds. Therefore, the form values must be in the URL so that I can process them correctly in the controller.
I've found this question here but that wasn't so helpful
How To Pass GET Parameters To Laravel From With GET Method ?
Thanks for your help!
To do this, you'll need to switch off CSRF token check, which is a bad idea. Or you could use JS to build the query which is not a good idea too.
The best way to handle this is to use POST instead of GET:
<form action="{{ route('test') }}" method="post">
And then change the route to:
Route::post('test', ['as' => 'test', 'uses' => 'MainController#test']);
You can do this via javascript.
You don't need to use form like this.you can just get input values by id(getElementById) and on a button click, format them as you expect (test/{param1}/{param2}/{param3}) and redirect page to that.
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>