String getting cut when converted from php to html [duplicate] - php

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."'" : ""}}>

Related

How do I keep checkbox checked after refresh

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

How to access a Request parameter in Laravel blade?

I'm trying to set/unset the HTML checked attr based on the presence of a query string, but Laravel is throwing a syntax error:
<input type="checkbox" name="my_checkbox" class="form-check-input" id="priority" {{ Request::get("my_checkbox") ? checked : null }} >
Somehow this doesn't parse correctly and I get a syntax error on one of the closing brackets:
syntax error, unexpected ')'
You should do that :
<input Type="checkbox" ="my_checkbox" class="form-check-input" id="priority" {{ Request::get("my_checkbox") ? 'checked' : null }} >

How cast or convert array value string to boolean in php

I'm Working with Laravel ACL.In every Role have group of permissions. I represent the all permissions using checkbox. When I submit form it's return a permissions array like this.
{
read: "true",
create: "true",
delete: "false",
update: "false"
}
array value show as double quotes string ,but i want it as boolean
How can i cast array values string to boolean.
#foreach($role->permissions as $key=>$value)
<td>
<input type="hidden" name="permission[{{$key}}]" class="role" value="false" {{ $value==0 ? 'checked' : '' }}>
<input type="checkbox" name="permission[{{$key}}]" class="role" value="true" {{ $value==1 ? 'checked' : '' }}>
</td>
#endforeach
I want to array value like this.
{
read: true,
create: true,
delete: false,
update: false
}
HTML double quotes values. If your values are always going to be "true" or "false" then you can specifically check for "true".
// change your json into an array
$values = json_decode($yourValues, true) ;
// loop through and check for "true"
foreach($values as $key=>$value) {
$values[$key] = ($value == "true") ;
}
// if you need it as json again
$yourValues = json_encode($values) ;
For you case,
#foreach($role->permissions as $key=>$value)
<td>
<input type="hidden" name="permission[{{$key}}]" class="role" value="false" {{ (boolean)$value ? 'checked' : '' }}>
<input type="checkbox" name="permission[{{$key}}]" class="role" value="true" {{ (boolean)$value? 'checked' : '' }}>
</td>
#endforeach
Try like this
####edit####
$data=$_POST[];
foreach($data as $record){
$record=(boolean)$record;
//now $record is boolean
}
try like this
#foreach($role->permissions as $key=>$value)
<td>
<input type="hidden" name="permission[{{$key}}]" class="role" value=false {{ $value=='false' ? 'checked' : '' }}>
<input type="checkbox" name="permission[{{$key}}]" class="role" value=true {{ $value=='true' ? 'checked' : '' }}>
</td>
#endforeach

How to get old input with value 0 in laravel?

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";} ?>

Laravel form checkbox checked flag not working correctly

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

Categories