how to fetch data from form's input and pass it into same form's action laravel - laravel

So my code looks like this:
<form method="get" action="{{URL::to('users', $_GET['username'])}}">
<input type="text" name="username">
<input class="but-submit" type="submit" name="submit">
</form>
I want my link to look like users/anydata, so that I could use it to fetch info from database.
My controller look like this:
Route::resource('users', 'UsersController');

I hope this can help you.
--Write a JavaScript function and update the URL
<form name="mForm" method="get" onSubmit="formSubmitAction(this)">
function formSubmitAction()
{
var username = document.getElementsByName("username").value;
document.mForm.action = "http://yourURL.com/"+username;
document.myForm.submit();
}

Related

put query params inside a filter key laravel

hi i want to change this url from get form
http://127.0.0.1:8000/allfiles?category_id=1&file_id=1
to this in laravel
http://127.0.0.1:8000/allfiles?filter[category_id]=1&filter[file_id]=1
how can i do that?
Actually i want change perquery=? to filter[perquery]=?
thanks
Try something like this in the blade (you can modify it if need):
<form action="http://127.0.0.1:8000/allfiles" method="GET">
<input name="filter['category_id']">
<input name="filter['file_id']">
<input type="submit">
</form>

Confusion regarding action in the form

I am fairly new to Laravel and I am trying CRUD operations using Resource Controller. The problem I am facing is regarding what should be the action in create a task form. Let me give you an overview, how the application is designed. I have created separate directories for MVC as listed below:
Todo_Model\todo_model.php
Todo_Controller\todo_controller.php
Todo_View\home.blade.php
Todo_View\create.blade.php
Route: Route::resource('todo','Todo_Controller\todo_controller');
route:list
Controller:
public function index()
{
return view('Todo_View\home');
}
public function create()
{
return view('Todo_View\create');
}
public function store(Request $request)
{
$todo= new todo_model();
$todo->title=$request->title;
$todo->body=$request->body;
$todo->save();
return redirect('todo');
}
create.blade.php
<form method="POST" action="../todo">
{{csrf_field()}}
<tr><td><input type="text" name="title" value="" placeholder="Title"></td></tr>
<tr><td><input type="text" name="body" value="" placeholder="Body"></td></tr>
<tr><td><input type="submit" name="submit" value="Submit"></td></tr>
</form>
Now the problem is that the action of the form should be todo as can be seen in the route:list but when I hit submit with that I get MethodNotAllowedHttpException and the URL shown is http://localhost/laravel-7/blog/public/todo/todo. But during the hit and trial I figured out I should use form action as ../todo. I am highly confused as to why do I have to use that action as it doesn't make any sense because in the route list, URI is clearly mentioned as todo
Another point, when I hit index page, URI is http://localhost/laravel-7/blog/public/todo and when I get redirected from home page to create page, the URI is http://localhost/laravel-7/blog/public/todo/create
You are confusing action with native php in laravel.
Replace your action="../todo" with action="{{url('todo')}}"
<form method="POST" action="{{url('todo')}}">
{{csrf_field()}}
<tr><td><input type="text" name="title" value="" placeholder="Title"></td></tr>
<tr><td><input type="text" name="body" value="" placeholder="Body"></td></tr>
<tr><td><input type="submit" name="submit" value="Submit"></td></tr>
</form>
You just need to specify the route exactly no need for saying where the file is actually located so action="/todo" would work fine

laravel 5.5 | old() empty in view unless $request->flash() used

I've run into an odd issue where the helper function old() always returns null in a blade view unless $request->flash() is used prior to loading the view. I have never had to do this when using laravel in the past. Did something change or is there something that I have forgotten to set/configure. Below is a simple example of the behavior:
web.php
Route::get('/test', function(){
return view('testView');
});
Route::post('/test', function(Illuminate\Http\Request $request){
$request->flash(); // if uncommented old() works, if commented old() does not work
return view('testView');
});
form in testView.blade.php
<form action="/test" method="POST">
{{csrf_field()}}
<input type="hidden" name="test001" value="001"/>
<input type="hidden" name="test002" value="002"/>
<div class="">
{{old('test001')}}
<br/>
{{old('test002')}}
</div>
<button type="submit">GO</button>
</form>
after form submitted without $request->flash()
after form submitted with $request->flash()
EDIT
Thinking this might have something to do with using a single route name for both post and get methods, the form was changed so to submit via get, and the issue persists. For example:
web.php
Route::get('/test', function(function(Illuminate\Http\Request $request){
return view('testView');
});
form in testView.blade.php
<form action="/test" method="GET">
<input type="hidden" name="test001" value="001"/>
<input type="hidden" name="test002" value="002"/>
<div class="">
{{old('test001')}}
<br/>
{{old('test002')}}
</div>
<button type="submit">GO</button>
</form>
Use redirect back() instead of loading view directly in a post method.
return redirect()->back()->withInput();
You need to flash request data to put old input into session, otherwise old() will return empty result. See official doc here.

Cross Domain Form Submission in Laravel

It is my first time to use laravel. I just want to ask a question regarding cross domain form submission best practice.
Let say I have simple HTML form in myfirstdomain.com/form
<form action="http://myseconddomain.com/insert/data/" method="POST">
<input name="fullname" type="text" placeholder="Enter your name" />
<input type="submit" value="submit">
</form>
And I have register "/insert/data" on my route file in other domain, let say myseconddomain.com :
Route::get('/insert/data', function()
{
$fullname = Input::get('fullname');
// Insert the name after this...
});
Is it safe to do that? I mean I'm using POST to submit form and using Route::get to fetch it?
Is there any other better option? I tried Route::post and get the MethodNotAllowed error instead.
Thank you

Make form use ajax

I am having issue making a form be sent using ajax.
Here is my code:
<form id="form" method="get">
<input type="text" name="name" id="name"><br>
<input type="text" name="email" name="email"><br>
<input type="submit" id="submit" value="submit">
</form>
$('#submit').click(function(event){
alert('ajax');
});
The "ajax" alert shows, but then the page gets reloaded! How can I stop this behaviour?
You have to use preventDefault
$('#submit').click(function(event){
event.preventDefault();
alert('ajax');
});
use $.submit() instead of click :)
$('#form').submit(function(event){
alert('ajax');
// here some ajax functions for sending via get or post ;)
return false; // this stops loading the action site.. its something like e.preventDefault() on links
});

Resources