How to get old input with value 0 in laravel? - php

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

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 post blank array (neither NULL nor "") values in php in no checkbox is checked

I have a list of checkboxes and if checked their values are posted to json file which is working fine. When user uncheck all of them I get NULL value in array and if I set its value="" its returns depends :[""]. what I want is if user did not check any checkbox it returns depends:[] only neither NULL nor "".
Here is my code:
<label for="depends">Linked With</label></br>
<input type="checkbox" name="depends[]" value="1" id="ch2" > 1
<input type="checkbox" name="depends[]" value="2" id="ch3" > 2
<input type="checkbox" name="depends[]" value="3" id="ch5" > 3
<input type="checkbox" name="depends[]" value="4" id="ch6" > 4
<input type="checkbox" name="depends[]" value="5" id="ch7" > 5
<input type="checkbox" name="depends[]" value="6" id="ch8" > 6
<input type="checkbox" name="depends[]" value="7" id="ch9" > 7
in php code I am calling it as
'depends'=>$_POST['depends']
in your php code you can handle it like that :
<?php
/* if $_POST['depends'] is not empty then you store the value, else you create an empty array [] */
$var = isset($_POST['depends']) ? $_POST['depends'] : array(); /* you can replace $var */
print_r($var);
?>

display radio button value from sql on page load

Making a form to edit sql table, form uses radio button and so user can see the existing value, when page loads, I would like my radio buttons to be indicated on/off according to the db. There are 2 buttons, they send values 0 & 1. I'm currently using isset with no success. Is there a better (correct) way to do this? My php is working for the text fields, but with the radio nothing is selected.
<label>
<input type="radio" name="transaction" value="0" <?php if (isset($transaction) && $transaction === '0' ) { echo 'checked="checked"';}?>/>
Listings</label>
<br />
<label>
<input type="radio" name="transaction" value="1" <?php if (isset($transaction) && $transaction === '1' ) { echo 'checked="checked"';}?>/>
Transaction History</label>
Most likely the value of $transaction is a number, not a string. Change the comparison to check against a number, not against a string:
<input type="radio" name="transaction" value="0" <?php if (isset($transaction) && $transaction === 0 ) { echo 'checked';}?>/>
<input type="radio" name="transaction" value="1" <?php if (isset($transaction) && $transaction === 1 ) { echo 'checked';}?>/>
Alternatively, use the weak equality == operator to just compare the values, not the types:
<input type="radio" name="transaction" value="0" <?php if (isset($transaction) && $transaction == '0' ) { echo 'checked';}?>/>
<input type="radio" name="transaction" value="1" <?php if (isset($transaction) && $transaction == '1' ) { 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);

Radio Button Array - Need Help

I have a radio button array that I need help with. Here is the code:
<input type="radio" name="radio" id="academic" value="1"<?php
if ($row_EventInfo['academic'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['academic'] = '';}
?>>
<label for="academic">Academic</label><br />
<input type="radio" name="radio" id="personal" value="1"<?php
if ($row_EventInfo['personal'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['personal'] = '';}
?>>
<label for="personal">Personal</label><br />
<input type="radio" name="radio" id="diversity" value="1"<?php
if ($row_EventInfo['diversity'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['diversity'] = '';}
?>>
<label for="diversity">Diversity</label><br />
What I'm trying to do is this. I have a column in my database table for each radio button because we have to have their inputs separately. However, I want them to only be able to select one of the buttons at a time. I changed all the names to be the same ("radio"), but since PHP MYSQL uses the names to know where to post the information in the table it doesn't know where to post.
Is there any way to create an if statement to tell it to only allow one button at a time to be selected and keep the inputs separate for the database table?
Please let me know if you need clarification. Thanks!
set the value to the column name, eg
<input type="radio" name="radio" id="diversity" value="diversity"
on the php end, simply do something like
$sql = "UPDATE table SET {$_POST['radio']} = 1";
this is in a form, right?
Then you get the data in the POST. I wouldn't set the value to 1, set the value to the value you want to select, e.g.
Then in the post, get the value and use this to set the data in the DB.
Hope this helps!
BR,
TJ
If you use:
<input type="radio" name="radio" id="diversity" value="value1">
<input type="radio" name="radio" id="diversity" value="value2">
<input type="radio" name="radio" id="diversity" value="value3">
<input type="radio" name="radio" id="diversity" value="value4">
it will be available in $_POST['radio']; with what ever radio button you selected. eg.value3 dont confuse yourself by naming input types with the same name
If you use:
<input type="radio" name="myradio" id="diversity" value="value1">
It will be available in $_POST['myradio'];
and so on
plus you may want to look into using the ternary operator
if ($row_EventInfo['diversity'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['diversity'] = '';}
into
echo ($row_EventInfo['diversity'] == '1') ? ' checked="checked"':'';

Categories