how can I auto check the checkbox based on the value from the database? I have the following code:
<input type="checkbox" name="CarModel" value="Yes" ($row[CarModel]==Audi? 'checked' : '') >
it should be like this
<input type="checkbox" name="CarModel" value="Yes" <?php echo ($row['CarModel'] == 'Audi' ) ? 'checked' : NULL ; ?> >
Related
I have a checkbox that is always checked no matter if I checked the box or not.
Here is how the checkbox is currently setup:
<input class="classname" id="check" value="1" <?php echo (($temp['test']) ? 'checked' : ''); ?> name="check" type="checkbox" />
Here is how the check box value is being captured:
$check = $_POST['check'] ?? 0;
I also tried the following code as well:
$check = (isset($_POST['check']) == '1' ? '1' : '0');
I tried the hidden field approach as well and it didn't work. Any ideas on why the checkbox is showing marked even though I removed the check.
I think Drussy might be onto something, where does $_POST['test'] come from?
Try this code:
$checked = isset($_POST['check']) ? 'checked' : '';
<input class="classname" id="check" value="1" <?php echo $checked; ?> name="check" type="checkbox" />
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);
?>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a variable called music. I would like if that variable = no the no radio button is checked and if it = yes the yes radio button is checked. Any ideas? Here's what I have so far but isn't working:
<?php
$music = $venue['music'];
echo $music;
?>
No<input type="radio" name="music" value="No" <?php $music == 'No' ? 'checked' : '' ?>/>
Yes<input type="radio" name="music" value="Yes" <?php $music == 'Yes' ? 'checked' : '' ?>/>
You are not outputting the 'checked' string. Either use echo or <?= ?>.
For example:
No <input type="radio" name="music" value="No" <?php echo ($music == 'No' ? 'checked' : ''); ?>/>
Yes <input type="radio" name="music" value="Yes" <?= ($music == 'Yes' ? 'checked' : '') ?>/>
<?php
$music = $venue['music'];;
if($music == "Yes" || $music == "yes")
{
?>
<input type = "radio" checked="checked">
<label>Yes</label>
<?php
}else
{
?>
<input type = "radio">
<label>No</label>
<?php
}
?>
checked="checked" sets your radio button to enabled by default
Your logic is correct, but you're just discarding the returned
expressions. You need to echo the string 'checked', either with the
language construct itself or with the short echo tags <?= ?>. Some
alternatives:
<input type=" ... " <?php echo $music == 'No' ? 'checked' : null ?> />
<input type=" ... " <?= $music == 'No' ? 'checked' : null ?> />
<input type=" ... " <?php if ($music == 'No') echo 'checked' ?> />
You just put "checked" : 'checked'
Please find the code below: in my case i have put 'yes' as checked:
No <input type="radio" name="music" value="No" <?php echo ($music == 'No' ? 'checked' : ''); ?>/>
Yes <input type="radio" name="music" value="Yes" <?php echo ($music == 'Yes' ? 'checked' : 'checked') ?>/>
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 am having a problem in retrieving checkboxes from the database.
Actually I am making edit form by using the input form which insert the values in database.
I am facing problem with checkboxes and other inputs works fine.
My code looks like
<input type="checkbox" name="proizvodi[]" value="0" <?if ($row['PROIZVODI'] == '0') $checked = 'checked="checked"'; ?> checked="checked"><b>product1<b/> <input type="checkbox" name="proizvodi[]" value="1" <?if ($row['PROIZVODI'] == '1') $checked = 'checked="checked"'; ?> checked="checked"><b>product2<b/> <br/>
<input type="checkbox" name="proizvodi[]" value="2" <?if ($row['PROIZVODI'] == '2') $checked = 'checked="checked"'; ?> checked="checked"><b>product3</b> <br/>
To make it more clear, I want only checkboes that are stored in db to be checked. for ex if value 1 is stored in db, product1 checkbox will be checked, if value 1 is no stored then check box will be not checked.
I appreciate any help or advice.
Thanks in advance!
How about this? You need to echo the 'checked="checked"' and do not assign it to a variable
<input type="checkbox" name="proizvodi[]" value="0" <?php if (strpos($row['PROIZVODI'],'0') !== false) echo 'checked="checked"'; ?>><b>product1<b/>
<input type="checkbox" name="proizvodi[]" value="1" <?php if (strpos($row['PROIZVODI'],'1') !== false) echo 'checked="checked"'; ?>><b>product2<b/> <br/>
<input type="checkbox" name="proizvodi[]" value="2" <?php if (strpos($row['PROIZVODI'],'2') !== false) echo 'checked="checked"'; ?>><b>product3</b> <br/>
Write your rows like this don't make it any harder than it is:
<input type="checkbox" name="proizvodi[]" value="0" <?php if ($row['PROIZVODI'] == '0') { ?> checked="checked" <?php } ?> /><b>product1<b/>