I've form with checkboxes:
{{ form_ajax('onAjaxList') }}
{% for soc in socs %}
<input type="checkbox" name="Filter[soc_id]" value="{{soc.id}}" id="
{{soc.name}}">
{% endfor %}
{{ endform() }}
And i tried get values from controller:
$options = post('Filter',[]);
but in $options i get only last checkbox value and i need full array checkboxes. How to do it?
Your input name should be like name="Filter[soc_id][]" instead of name="Filter[soc_id]" so change your input from
<input type="checkbox" name="Filter[soc_id]" value="{{soc.id}}" id="
{{soc.name}}">
TO this
<input type="checkbox" name="Filter[soc_id][]" value="{{soc.id}}" id="
{{soc.name}}">
When you are dealing with multiple checkbox you always need to use array.
so adding [] square brackets after name will make it array and add data index will start from 0 index.
if you need more control you can also pass name="Filter[soc_id][0]" , name="Filter[soc_id][1]", name="Filter[soc_id][2]" ... manually if needed to use saved record's id, now array key will be your record's id.
In your case your code should look like this
{{ form_ajax('onAjaxList') }}
{% for soc in socs %}
<input type="checkbox" name="Filter[soc_id][]" value="{{soc.id}}" id="
{{soc.name}}">
{% endfor %}
{{ endform() }}
and from server side if you want to use filter
you can use this (October CMS, laravel)
$options = post('Filter',[]);
$query->whereIn('soc_id', $options['soc_id']);
this will do the job, filtering based on soc_id.
if you have any doubts please comment.
Related
In the front end I have a list of players which I want to select by a checkbox to form groups. But how do I store each player ticked in the checkbox into an array?
This is my blade
#foreach ($players as $index=>$player)
<tr>
<td>{{ $index+1 }}</td>
<td>{{ $player->gender }}</td>
<td>{{ $player->name }}</td>
<td>{{ $player->handicap }}</td>
<td>
<input type="checkbox" name="Player" value="{{$player->id}}"><br>
</td>
</tr>
#endforeach
{{ $players->links() }}
Does anyone has an idea?
Your input field should be named Player[], with the square brackets. That indicates there are multiple values for that field, so each player will have "his own" checkbox.
When you submit a form that contains array like field to your controller, you can firstly validate it with:
"Player" => ["required", "array"] and you can also validate each value of the array with "Player.*" => ["required", "integer"]. These are sample validations, but you can do a lot more (or less).
Now, to access the values, just use $request->input('player') just like you would with any other input, except now you will get an array, which you can loop through and implement your own logic.
Tick checkbox based on condition
If you want to tick the checkbox based on certain condition, in your blade, you can add
<input type="checkbox" name="Player" value="{{$player->id}}" {{ $player->name == 'Mark' ? 'checked' : '' }} />
I'm doing a filter-by specifications on an app, and was wondering how can I implement without reloading the whole page (which I'm trying to convert to AJAX rather than PHP/Laravel alone). However my filters are based off on foreach loops
#foreach($cores as $core)
<li style="list-style: none;">
<input type="checkbox" name="core[]" id="cores" class="form-check-control" value="{{ $core->value }}"> {{ $core->value }}
</li>
#endforeach
Am trying $('#cores').val() but it is not working. I'm thinking of call it by class name then looping it to check if it is checked but it may affect performance.
The for loop is creating multiple elements with the same id, so accessing it this way won't work. Remove the id element from the checkbox, and refer to this answer for getting the value of the checked boxes in JS
try to do it this way and u can take the values
#foreach($cores as $core)
<li style="list-style: none;">
<input type="checkbox" name="core[{{ $core->value }} || paste something unique for the checkbox]" id="cores" class="form-check-control" value="1"> {{ $core->value }}
</li>
#endforeach
after that u can foreach $core and take only values that are seted and filter by them
what I'm trying to do is a filter radio button. When a user apply the filter based on a specific year, it goes into the controller to filter data results based on that year and return back to the view with new values. However when the page refreshes, the value filtered by the year is reflected, just that the radio button is unchecked and the default All radio button is checked.
What I did was store the radio button's id in a Session at the controller based on value of which radio button has been clicked. If the id of the radio button matches the id stored in the session, it should checked that radio button.
Index.blade.php
<label>
<input type="radio" name="year" id="tab" value="_all" class="flat-red filter" checked="checked"> All
</label>
#foreach ($years as $year)
<label>
<input type="radio" name="year" id="id{{$year->year}}" value="{{ $year->year }}" class="flat-red filter" checked = {{ Session::get('radio') == "id{{$year->year}}" ? 'checked' : 'false' }}> {{$year->year}}
</label>
#endforeach
<button type="submit" class="btn btn-primary btn-sm pull-right">Apply Filter</button>
StatisticsController.php
session(['radio' => 'id'.request('year')]);
return view('statistics.index', $counts, compact('years'));
Error that I am getting
syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM)
Method(s) I have tried,
1. Using JS to see which radio button has been click and store it in the local session.
What am I doing wrong or missed out?
Everything inside of {{ }} is PHP. The {{ }} gets replaced by the Blade Compiler with something like this:
<?php echo e(...); ?>
So basically just use regular PHP syntax between {{ }}:
{{ Session::get('radio') == "id{$year->year}" ? 'checked' : 'false' }}
Having {{ }} inside of {{ }} is going to break the compiling of that echo statement.
A solution could be to read that session variable in your controller and inject it into your template.
Don't put more logic into the template, that's not what a template engine is made for - better decouple such logic. This helps you to keep your code maintainable. A blade template does not need to know anything about a session
The code is like this:
{{ Form::hidden('id') }}
<td class="td-actions">
{{ Form::checkbox('active', 'active',true) }}
{{ Form::hidden('active', 'inactive',false) }}
</td>
{{Form::close() }}
On displaying the value in web browser I get both values-active & inactive in both ckecked & unchecked cases.
How can I get rid of that?
You should have the hidden input before the checkbox, and use numeric values rather than booleans:
{{ Form::hidden('active', '0') }}
{{ Form::checkbox('active', '1') }}
If the checkbox isn’t checked, then you will get a value of 0. If the checkbox is checked, then you will get the checkbox’s value (1).
Is there an easier way to output old form data with Twig in this example? Inside this ternary operator, it automatically escapes the value="" string which is undesired. I still want the old email value escaped.
<input type="text" name="email" id="email"{{ request.post('email') ? (' value="' ~ request.post('email')|e ~ '"')|raw : '' }}>
I'm not sure what request.post is, but assuming it behaves like Symfony's app.request.request.get (returning null if no such post param exists) you can simply do:
<input value="{{ request.post("email") }}" />
It'll be the value if it exists, or an empty value if it doesn't. If you are a purist and do not wish to see an empty value attribute, you can do this:
<input{% if request.post("email") %} value="{{ request.post("email") }}"{% endif %} />