0
am trying to check checkbox after i search categories. but its errors
explode(): Argument #2 ($string) must be of type string, array given
<input id="subCats-{{$s->id}}" name="category[]" type="checkbox" value="{{ $s->id }}"
#if (request()->category != "" && in_array($s->id,explode(',', request()->category)))
checked
#endif
>
and am having problem with
<input type="checkbox" name="level" value="0" id="lvl-0"
#if (request()->level == 0)
checked
#endif
it's always is checked because if i don't requests its null and it's thinks that its 0 i tried (request()->level === 0) but it's still don't works.
others with level are working
<input type="checkbox" name="level" value="1" id="lvl-1"
#if (request()->level == 1)
checked
#endif
<input type="checkbox" name="level" value="2" id="lvl-2"
#if (request()->level == 2)
checked
#endif
The problem is your
in_array($s->id,explode(',', request()->category)))
explode generates an array from a string, but your "request()->category might not give you a string, it is already an array.
so maybe its working with this:
in_array($s->id, request()->category ?? []))
I think it's being checked regardless of the if statement. So this might work:
<input type="checkbox" :checked="{{ request()->level === 0 }}" />
To fix the problem with the null / 0, you can add to your condition a check to make sure the value is not null like this:
#if (request()->level == 0 && !is_null(request()->level))
checked
#endif
Related
This question already has answers here:
How to set HTML value attribute (with spaces)
(6 answers)
Closed 11 months ago.
I have the following string.
$onclick= " return false;";
And I'm trying to insert it in my HTML code like this.
<input type="checkbox" id="grabados" name="grabados[]" value="no" onclick="return false;">
with this code
<input type="checkbox" id="grabados" name="grabados[]" value="{{ (is_null($partida['grabado']) ? 'no' : 'si') }}" {{ (is_null($partida['grabado']) ? '' : ' checked') }} {{ $partida['tipo'] == "otro" ? "onClick = ".$onclick : ""}}>
instead I'm getting this
<input type="checkbox" id="grabados" name="grabados[]" value="no" onclick="return" false;>
When it gets to the space the string is cut
I have tried escape sequences, HTML functions, string functions, everything with no results.
A little help will be appreciated.
This should get closed as a typo shortly, as you've forgotten to delimit the attribute value. In the meantime, here's an example of how to do this while still having readable code:
<input
type="checkbox"
id="grabados"
name="grabados[]"
#if (is_null($partida['grabado']))
value="no"
#else
value="si"
checked="checked"
#endif
#if ($partida['tipo'] === "otro")
onClick="{{ $onclick }}"
#endif
/>
Also note, if you're creating multiple of these, you are repeating your id attribute, which is not allowed.
Try This
<input type="checkbox" id="grabados" name="grabados[]" value="{{ (is_null($partida['grabado']) ? 'no' : 'si') }}" {{ (is_null($partida['grabado']) ? '' : ' checked') }} {{ $partida['tipo'] == 'otro' ? "onClick = '".$onclick."'" : ""}}>
There are two associative arrays of different lengths, how to compare their values and apply to checkboxes? If the values match, I need the checkbox gets the attribute checked.
Now I have this:
Politics
Video (Checked)
Policy (Checked)
Video
I want
Video (Checked)
Policy (Checked)
My code at the monent:
#foreach($category as $categories)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]"> {{$categories->title}}
#foreach($post->category as $selected)
#if($selected->title == $categories->title)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" checked> {{$categories->title}}
#endif
#endforeach
#endforeach
You can apply that condition inside element to apply checked property
#foreach($category as $categories)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]"> {{$categories->title}}
#foreach($post->category as $selected)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" {{$selected->title == $categories->title ? 'checked' : ''}}> {{$categories->title}}
#endforeach
#endforeach
I have resolved this problem
Controller:
$checkedCategory = [];
foreach($post->category as $selected) {
for($i = 0; $i < count($category); $i++) {
if($selected->title == $category[$i]->title) {
$checkedCategory[$i] = $category[$i]->title;
}
}
}
$checkedCategory = array_values(array_unique($checkedCategory, SORT_REGULAR));
Template:
<fieldset>
<legend>Категории</legend>
#foreach($category as $categories)
#if(in_array($categories->title, $checkedCategory))
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" checked> {{$categories->title}}
#else
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]"> {{$categories->title}}
#endif
#endforeach
</fieldset>
I have two array $softwarelist and $virt_software_select when i use #foreach in blade template like below code i got checked but $softwarelist is double in label.
$softwarelist =["Adobe Photoshop","Adobe Illustrator","Corel Draw","My SQL","Oracle"];
$virt_software_select=["Adobe Photoshop","My SQL"];
#foreach($softwarelist as $slst)
#foreach($virt_software_select as $sw_usr)
#if($slst->software==$sw_usr->virtualize_software)
<label>
<input type="checkbox" checked name="virt_software[]" value="{{$slst->software}}"/>
{{$slst->software}}
</label>
#else
<label>
<input type="checkbox" name="virt_software[]" value="{{$slst->software}}"/>
{{$slst->software}}
</label>
#endif
#endforeach
#endforeach
i got these
How can i get checked the same data in two array but not double data with $softwarelist in laravel?
You have to use in_array() php function then it will work and use only one foreach
$softwarelist =["Adobe Photoshop","Adobe Illustrator","Corel Draw","My SQL","Oracle"];
$virt_software_select=["Adobe Photoshop","My SQL"];
#foreach($softwarelist as $slst)
#if(in_array($slst,$virt_software_select))
<?php $checked ="checked"; ?>
#else
<?php $checked =""; ?>
#endif
<label>
<input type="checkbox" {{$checked}} name="virt_software[]" value="{{$slst->software}}"/>
{{$slst->software}}
</label>
#endforeach
Hope it helps!
You have to use in_array() function instead of using nested foreach
$softwarelist =["Adobe Photoshop","Adobe Illustrator","Corel Draw","My SQL","Oracle"];
$virt_software_select=["Adobe Photoshop","My SQL"];
#foreach($softwarelist as $slst)
<label>
#if(in_array($slst,$virt_software_select))
<input type="checkbox" checked name="virt_software[]" value="{{$slst->software}}"/>{{$slst->software}}
#else
<input type="checkbox" name="virt_software[]" value="{{$slst->software}}"/>{{$slst->software}}
#endif
</label>
#endforeach
To redirect back with previous input, it is common to have return redirect()->back()->withInput();. Then, to retrieve the previous value for radio button:
<input name="question1" value="0" type="radio" id="q1-0" {{ old('question1') == 0?'checked':'' }}/>
<label for="q1-0" class="label-margin">0</label>
<input name="question1" value="1" type="radio" id="q1-1" {{ old('question1') == 1?'checked':'' }}/>
<label for="q1-1" class="label-margin">1</label>
<input name="question1" value="2" type="radio" id="q1-2" {{ old('question1') == 2?'checked':'' }}/>
<label for="q1-2" class="label-margin">2</label>
<input name="question1" value="3" type="radio" id="q1-3" {{ old('question1') == 3?'checked':'' }}/>
<label for="q1-3" class="label-margin">3</label>
It works fine except for the one with value 0. When there is no previous input for question 1, old('question1') also return 0, making the choice 0 always chosen when return, where I want to keep all choices not chosen.
Is there a way to leave choice 0 not blank when there is no previous input?
try this. :)
if (old('input_name') !== null && old('input_name') == 0) {
// input is satified
}
You can try by comparing using 3 "=" like this : {{ old('question1') === 0 ? 'checked' : '' }}.
This will make sure that old('question1') is really 0 and not false or null.
Do like this
<?php if( old('question1) == '0'){echo "checked";} ?>
I have a piece of template:
#foreach ( Permission::all() as $permission )
{{ Form::checkbox('permissions[]', $permission->id, $role->permissions->contains($permission->id) ) }} <label>{{ $permission->label }}</label>
{{ var_dump($role->permissions->contains($permission->id)) }}
#endforeach
this outputs the following html:
<input checked="checked" name="permissions[]" type="checkbox" value="1"> <label>View entity</label>
<small>boolean</small> true
<input checked="checked" name="permissions[]" type="checkbox" value="3"> <label>Edit Entity</label>
<small>boolean</small> true
<input checked="checked" name="permissions[]" type="checkbox" value="4"> <label>Delete entity</label>
<small>boolean</small> false
W T H is going on. anyone any ideas?
EDIT
even moreconfusion. If I pass in false to the Form::checkbox function. Only the first item is rendered without the checked parameter. I give up :(
Found the 'problem'. See this issue on github: https://github.com/laravel/framework/issues/5078 :
my model being parsed has the permissions() array. The false will be ignored and the checkbox will be shown checked. Even if the $permission->id is not related to the Role.
FormBuilder#getCheckboxCheckedState
return is_array($posted) ? in_array($value, $posted) : (bool) $posted;
this line will return true, even if the $permission->id is set. Or the 'checked state' parameter is given.
Perosnally I think that all those isChecked checks should only be performed if $checked is_null.
FormBuilder#checkable()
$checked = $this->getCheckedState($type, $name, $value, $checked);
should be
if ( is_null($checked) ) $checked = $this->getCheckedState($type, $name, $value, $checked);