Laravel form not sending request of dynamically generated content - php

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>');

Related

Laravel - get data from input field not working

i'm having trouble to get data from an input.
I try to be more specific
My application has many views, and each of these has an #include component that works as a search field.
For example the user types in the input the ID of the store, the controller compares the ID that user inserted with the DB store's ID and then compacts data and fill the views with infos of that specific store.
Im just testing how to get that data from the input but i'm getting this error:
Route [search.get_kcli] not defined.
Actually i'm trying to use that function only for get data by using a controller only for that input field.
What's wrong in it?
Thanks for help!
My code looks like this:
inside of app.blade.php
#auth
#include('partials.search')
#endauth
inside search.blade.php
<form method="POST" class="form-inline position-relative"
action="{{ route('search.get_kcli') }}">
#csrf
#method('POST')
<input class="form-control shadow-none" name="kcli" id="kcli" type="number"
placeholder="Codice..." aria-label="Search">
<button type="submit" class="btn btn-light search-btn"><i class="fas fa-search"></i></button>
</form>
Inside the SearchController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class SearchController extends Controller
{
function get_kcli(Request $request) {
$kcli = $request->input('kcli');
dd($kcli);
}
}
Inside web.php
Route::post('/search', [App\Http\Controllers\SearchController::class, 'get_kcli'])->name('search');
Change
<form method="POST" class="form-inline position-relative"
action="{{ route('search.get_kcli') }}">
to
<form method="POST" class="form-inline position-relative"
action="{{ route('search') }}">

Laravel 8 - Request does not retrive input field

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>

Trying to submit one <form> with method="POST" duplicates the route and doesn't save the information with an store function()

Im trying to submit one form with method post to save one element in astore function() but when I click submit duplicates the route, for example looking like this:entradas/1/ventaEntrada/200000/montoTotal/entradas/1/ventaEntrada/200000/montoTotal and doesn't save the information, I dont know why this is happening
Below i will let the code of my route,my form,my create function() and my store function()
Also show404 Not Found when I click submit
Route
Route::resource('/entradas/{id_entrada}/ventaEntrada/{precio_entrada}/montoTotal', 'Venta_entradaController');
create function()
public function create(Request $request,$id_entrada,$precio_entrada){
$venta_entrada = DB::select(DB::raw(
"SELECT monto_total,fecha,fk_cliente_natural,fk_cliente_juridico
FROM venta_entrada "
)
);
return view('home.crearVenta_entrada')
->with('venta_entrada',$venta_entrada)
->with('id_entrada',$id_entrada)->with('precio_entrada',$precio_entrada);
}
store function()
public function store(Request $request,$id_entrada,$precio_entrada)
{
$venta_entrada=new Venta_entrada();
$venta_entrada->monto_total=$precio_entrada+$request->monto_total;
$now = new \DateTime();
$venta_entrada->fecha=$now->format('d-m-Y');
$venta_entrada->fk_cliente_natural=1;
$venta_entrada->save();
return back();
}
Form with the method POST
<form action="entradas/{{$id_entrada}}/ventaEntrada/{{$precio_entrada}}/montoTotal" method="POST">
#csrf
<input type="number" name="monto_total" placeholder="Monto total" class="form-control mb-2" required>
<button clas="btn btn-primary btn-block" type="submit">ADD</button>
BACK
</form>
I have check your problem and you have use "Resource Route" right.
so for routing run following command in your terminal/command prompt
php artisan route:list
It will show all routes lists and find your action name
Then you'll add in form action name like
<form action="{{ route('name') }}" method="post">
...
</form>
Example:
<form action="{{ route('Venta_entrada.store') }}" method="post">
...
</form>
I hope it will help you :)

why comming from <a> route "ruta/{{$var}}/edit" and a 'action': "ruta/{{$var}} my final route is: ""ruta/{{$var}}/"ruta/{{$var}}"?

I have a view whit the next code:
<h1>Edicion</h1>
#foreach ($usuarios as $usu)
<h4>{{$usu->nombre}}</h4>
editar
#endforeach
This route:
Route::resource('/prurequests','PruebasControllers\PrurequestsController');
The method edit:
public function edit($slug)
{
$usuario = Usuario2::where('slug','=',$slug)->firstOrFail();
return view('vistaspruebas.edit', compact('usuario'));
}
In this route my URL is:
/public/prurequests/vaca/edit
The code in this view 'vistaspruebas.edit' is:
<form action="prurequests/suma" method="POST">
#method('PUT')
#csrf
<label for="nombre">ingrese nombre</label>
<input type="text" name="nombre" value="{{$usuario->slug}}">
<br />
<button type="submit" name="" value="submit">Actualiza</button>
</form>
instead of looking for this route: "prurequests/suma " Laravel look for /public/prurequests/vaca/prurequests/suma
someone knows why after the tag and call other route it removes 'edit' and change it by and other route i put here?
Please use action() helper method in your form as the following:
<form action="{{ action('Controller#method') }}" method="POST">
Or you can use the route helper method as the following:
<form action="{{ route('route_name') }}" method="POST">
Also you can use url() helper if you want to use a given path:
<form action="{{ url('path_here') }}" method="POST">

Symfony2 form : how to get the submited value in the url?

what I need is "onSubmit" to get the "query" value, and put it in the "_search_all" route, normally this would look like => {{ path('_search_all', {'slug': query_val} ) }} this way I can get a route like this in my browser localhost/search-all/symfony2
The current form
<form class="searchbar" method="POST" action="{{ path('_search_all') }}">
<input name="submit" type="submit" value="Search"/>
<span>
<input type="text" name="query" required/>
</span>
</form>
Solutions so far:
method="GET" however this will create the "ugly" looking url
use the friendsofsymfony/jsrouting-bundle
any inputs are welcome
You need to use onSubmit event and set a javascript variable which contains the path; as sample try something like:
var frm_action = "{{ path('_search_all', {'keyword' : ''}) }}";
$(document).ready(function() {
$('#myForm').attr('action', frm_action + $('input[name="query"]').val());
});
and your need to add the ID to your form:
<form class="searchbar" method="POST" id="myForm" action="" >

Categories