Laravel - How to get the value of input in a view - php

I'm building a page with 2 forms like this:
index.blade.php:
<form action="/" method="post">
<input type="text" list="cats" name="catipt" id="catipt" />
<datalist id="cats">
#foreach($categories as $category)
<option>{{ $category->name }}</option>
#endforeach
</datalist>
<button type="submit">Add</button>
</form>
#include("catForms")
catForms.blade.php contains a form aside from the first one, with a send button to send the form input via email. The second form in which there are many div that each has input for the user to fill in. The first form allows user to add items to the dropdown list, so it's a post method. Based on the input in the first form, a corresponding div in second form will show, others will hide, using Jquery. Now, I want to send the data from the user via email, but how do I get the value of the input in the first form in the email view so that I can filter out the irrelevant div? Otherwise, I'll have to list out all div even they are empty.
The problem is that, there are two forms, so there are two Request $request, I can't get the input value from the first form in email view. I tried this in email view, but this doesn't work:
#php
use Symfony\Component\Console\Input\Input;
#endphp
Cat: {{ Input::get('catipt') }}

<form action="/" method="post">
<input type="text" list="cats" name="catipt" id="catipt" />
<datalist id="cats" name="cat-name">
#foreach($categories as $category)
<option value="{{ $category->name }}">{{ $category->name }}</option>
#endforeach
</datalist>
<button type="submit">Add</button>
</form>

Related

Problem of recovery of an element selected in a select

I am a beginner in Laravel. I want to implement a feature that:
I want to use Laravel 8 and PHP:
Select a part of the data in a table (via a select)
how to get the id selected in the select :
-> get the identifier selected in the option tag in order to use it in an sql query that combines selection and insertion
Copy the data and insert it in another table (via an add button)
how to recover the id present in the url of the page
Display the data inserted in a table
no problem
<select name="id_segment" class="form-control">
<?php
$req1 = DB::select("SELECT id_segment, nom_segment FROM `segements` ");
foreach ($req1 as $re1 => $r1) {
?>
<option name="id_segment" value="{{ $r1->id_segment }}"> {{ $r1->id_segment }}
{{ $r1->nom_segment }}</option>
<?php
} ?>
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary" id="AjoutSegment">Ajouter</button>
<a class="btn btn-primary" href="{{ route('groupecampagnes.index') }}"> Back</a>
</div>
I have already tried with $_POST and $_GET while making a var_dump, but I have seen that some people say that it does not work, me it gave me the display of an error with the id that I seek to recover precisely, whereas it should just display it to me on the page to see if I recover it well and truly. The var_dump is not present in the code because I removed it in the meantime. After I tried various solutions found on the net, which have me display as error for some of them that I had to do with a std class object.
Assuming your code is in a file called page and stored in the views directory (resources/views/page.blade.php), you could go to your controller and pass the needed data to your view like this:
$segments = DB::select("SELECT id_segment, nom_segment FROM `segements` ")->get();
return view('page', compact('segments'))
Then you can use it in your view:
<select name="id_segment" class="form-control">
#foreach ($segments as $r1)
<option name="id_segment" value="{{ $r1->id_segment }}"> {{ $r1->id_segment }} {{ $r1->nom_segment }}</option>
#endforeach
</select>

laravel update without passing ID

I am working on a ticket log where tickets get edited multiple times before they are 'done'.
I have an overview where you see all the tickets, on top of the page are 6 buttons. All buttons open a popup with a form. The first button is a create function which makes the ticket and fills in the first information. All other fields are now NULL. The second buttons is the first edit button.
Upon opening it opens an form where the user scans a barcode to fill in the first field; Ticket Number. this ticket number is unique for every Ticket. Next thing to fill in is your credentials, which come from a dropdown.
Due to the fact that all the edit forms work by Ticket_number (which is scanned from a QR code) I made it so all forms are on the same page. Therefor I am not able to do something like : action="{{ route('countries.update',$country->id) }} as I don't know the ID at this moment.
Here is the code behind everything(and you can see what I tried there too):
web.php:
// TICKET LOG ROUTES
Route::post('ticket/start-picking', 'TicketController#startpicking')->name('tickets.startpicking');
Route::resource('tickets', 'TicketController');
form for edit:
<div id="modal-start-picking" class="modal">
<form method="POST" id="modal-start-picking-form" action="{{ route('tickets.startpicking') }}">
#csrf
<input type="text" value="modal-start-picking" name="type" hidden>
<div class="modal-content">
<h4>Start Picking</h4>
<p>Scan Ticket Number here</p>
<div class="input-field">
<label for="start">Ticket Number</label>
<input type="text" name="ticket_number" id="start" autofocus>
</div>
<div class="input-field">
<select id="picker" name="picker">
<option value="" disabled selected>Choose a Picker</option>
#foreach($employees as $employee)
<option {{(old('picker') == $employee->initials ) ? 'selected' : ''}} value="{{ $employee->initials }}">{{ $employee->initials }}</option>
#endforeach
</select>
</div>
</div>
<div class="modal-footer">
<input class="btn-flat" type="submit" value="Start Picking">
</div>
</form>
</div>
Function in TicketController:
public function startpicking(Request $request, Ticket $ticket)
{
$request->validate([
'picker' => 'required',
]);
$ticket->update(array_merge($request->all(), ['picker_start_time' => Carbon::now()]));
return redirect()->route('tickets.index')->with('toast', 'Ticket updated successfully');
}
If any more info is required I Will gladly update my post.
I found out how it's done.
$request->validate([
'picker' => 'required',
]);
Ticket::where('ticket_number', $posted_ticket_number)->update([
'picker' => $request->picker,
'picker_start_time' => Carbon::now(),
]);
You just have to use ::where('column', 'variable').
The variable was pulled from my form (The Ticket number in my case. As this was an unique identifier.) and the column is self explanatory.

Laravel: Two Actions in a Form - one with "on-change" one with a button?

in my Laravel App I have a Blade "Insert Project" where I want to dynamic load html data from Database when a DropDown changes. To be more concrete: I have several Project Categories in the Table "Cats" (id, name, code) - Each Category needs different fields to be added, therefore I have put the html code in the Table "Cats". I want to achieve the following:
If a user selects a Project Category the html code which is attached to the category should be shown.
My Code so far:
<form method="POST" action="{{ route('project-insert') }}">
#csrf
<form action="{{ route('filter') }}" method="post">
<label for="Cat">Kategorie</label>
<select class="form-control" name="cat_id" id="cat_id" data-parsley-required="true" onchange='this.form.submit()'>
#foreach ($cats as $sn)
{
<option value="{{ $sn->id }}">{{ $sn->name }}</option>
}
#endforeach
</select>
</form>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Add Project') }}
</button>
</div>
</div>
</form>
In my Controller I have add:
public function insertProject(Request $data) {
return $data['cat_id'];
}
public function filter(Request $request)
{
$cat_id = $request->input("cat_id");
return $cat_id;
$code = DB::table('cats')->where('id', '=', $cat_id);
var_dump($code->code);
}
When I change the DropDown, then the function "insertProject" will be called and not the desired function "filter".
How can I achieve it to call the correct function, or how can I achieve it to call "insertProject" only when the button has been pressed?
Thank you for your advice and help.
Stefan

How to get value of data attribute from button to controller in laravel

How can we get the data attribute value from button to controller in laravel
My button
<input type="submit" value="Add click" name="submit" id="submit" data-name="{{$value->name}}" data-click="{{$value->click}}">
i want to get data-click and data-name pass it to controller
$click=data-click;
$name=data-name;
from button attribute after submit form to controller
But the result not get the data-name and data-click value. How can we try this??
If you want to post those two data through Normal Form Post.Then use hidden input fields:-
<input type="hidden" value="{{$value->name}}" name="data-name"/>
<input type="hidden" value="{{$value->click}}" name="data-click"/>
Or:-
{{ Form::hidden('data-name', $value->name) }}
{{ Form::hidden('data-click', $value->click) }}
Now on Controller side you will get it as :-
$request->input('data-name')
$request->input('data-click');
You can use hidden field for this or you have to use ajax.
#if ($value != '')
{{ Form::hidden('somevalue', $value->name) }}
{{ Form::hidden('somevalueclick', $value->click) }}
#endif

Form Model Binding for checkboxes

I'm using Laravel 4.1 and in my app I need to show a form with pre filled checkboxes. But I try to do it with using Form Model Binding, it doesn't work.
{{ Form::model($user, array('route' => 'settings-notify')) }}
<div class="formRow form-horizontal_row">
{{ Form::checkbox('notify_visit', '1') }}
</div>
<div class="formRow form-horizontal_row">
{{ Form::checkbox('notify_rate', '1') }}
</div>
<div class="formSubmit">
{{ Form::submit('Save') }}
</div>
{{ Form::close() }}
Is there any way to make it working?
I ended up handling this in my controller because as #Ladislav Maxa says in their comment to #Antonio Carlos Ribeiro the database never updates. This is because empty checkboxes are not submitted with the form so no 'unchecked' value is available for updating the database.
The solution I used is to check for the existence of the field in the get object. If it's there the box was checked.
$notify_rate = Input::get('notify_rate') ? 1 : 0;
$data['notify_rate'] = $notify_rate;
Another way I have done this before was to create a text input with the same name and a value of 0 before the checkbox. If the checkbox is unticked the text input value is submitted, if the checkbox is ticked it's value is used.
<input name="notify_rate" type="text" value="0">
<input name="notify_rate" type="checkbox" value="1">
That's probably not valid HTML though and I prefer dealing with this server side.
Works fine for me, here's a test I just did here:
Route::get('test', function() {
$user = User::where('email','myemail#domain.com')->first();
Form::model($user);
$user->notify_rate = true;
echo e(Form::checkbox('notify_rate'));
$user->notify_rate = false;
echo e(Form::checkbox('notify_rate'));
});
This is what I got:
<input checked="checked" name="notify_rate" type="checkbox" value="1">
<input name="notify_rate" type="checkbox" value="1">

Categories