PHP variable defaults radio button checked [closed] - php

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') ?>/>

Related

Checkbox is always checked even not checked

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" />

php not echoing back radio value

I cannot get the radio value's to echo back as checked within the conditional block, it's got to be an issue with my syntax. I know you don't have the full code but I can assure you 100% that the conditions are all true. What am I missing?
<?php if(!empty($userRow[insurance_name2])):?>
<input class="form-control" type="text" id="insurance-name2"
name="insurance-name2"
placeholder="Insurance Name"
value="<?php echo $userRow[insurance_name2] ?>">
<input name="insurance-network-option2" type='radio'
value="in-network" <?= ($userRow['insurance_option2'] == "in-network") ? 'checked' : '' ?>>In-Network
<input name="insurance-network-option2" type='radio'
value="out-of-network" <?= ($userRow['insurance_option2'] == "out-of-network") ? 'checked' : '' ?>>Out-Of-Network
<br>
<br>
<?php endif?>
try this
<?php if(!empty($userRow['insurance_name2'])):?>
<input class="form-control" type="text" id="insurance-name2" name="insurance-name2" placeholder="Insurance Name" value="<?= $userRow['insurance_name2']; ?>" />
<input name="insurance-network-option2" type='radio' value="in-network" <?= ($userRow['insurance_option2'] == "in-network") ? 'checked' : '' ?>/>In-Network
<input name="insurance-network-option2" type='radio' value="out-of-network" <?= ($userRow['insurance_option2'] == "out-of-network") ? 'checked' : '' ?> />Out-Of-Network
<?php endif ?>
You did forget some quotes and to close the input tag. which is what I changed.
If it doesn't solve your problem let me know what errors you're getting.
Realized my issue. Beneath I had a hidden div containing the same code as the block within the php conditional. Reason being, if the element exists in the database, I wanted it to display. Otherwise, if I user clicked a certain button to add an insurance name and network option, it would remove the hidden field.
So I rearranged the code like so, and called removeAttribute method from within the php block. Works perfectly.
<div class="hidden" id="insurance2">
<input class="form-control" type="text" id="insurance-name2"
name="insurance-name2"
placeholder="Insurance Name"
value="<?php echo $userRow[insurance_name2] ?>">
<!-- <br>-->
<input name="insurance-network-option2" type='radio'
value="in-network" <?= ($userRow['insurance_option2'] == "in-network") ? 'checked' : '' ?>>In-Network
<input name="insurance-network-option2" type='radio'
value="out-of-network" <?= ($userRow['insurance_option2'] == "out-of-network") ? 'checked' : '' ?>>Out-Of-Network
<br>
<br>
</div>
<!--//insurance two-->
<?php if(!empty($userRow[insurance_name2])):?>
<?php echo"<script type='text/javascript'>document.getElementById('insurance2').removeAttribute('class');</script>"?>
<?php endif?>

Retrieve mysql value from php for radio buttons

As trivial as it can seems, I'm having problems retrieving values for radio buttons from MySql database through PHP. It's my first learning project so I'm trying my best
Question has already been asked but I found no useful answer.
The php code does a simple "Select *" so I retrieve all the fields.
This is the php code
<label>Owner: <?php echo $row['Owner']; ?></label></br>
<input type="radio" name="Owner" checked=<?php if($row['Owner'] = "A") { echo "true"; }?> value="A">A
<input type="radio" name="Owner" checked=<?php if($row['Owner'] = "B") { echo "true"; }?> value="B">B</br></br>
and I retrieve the values with mysqli_fetch_array().
This is the result:
As you can see the label retrieves the correct value, the radio buttons not.
I've already tried putting == instead of = and putting ' instead of " but I don't know why the checkbox "B" is checked, since Owner value is A.
Also, if there are any best practices which are better than this, you're welcome.
The HTML attribute checked should not get a value, its mere presence indicates that the radio button is checked. So do this:
<label>Owner: <?php echo $row['Owner']; ?></label></br>
<input type="radio" name="Owner" <?php if($row['Owner']=="A") {echo "checked"}?> value="A">A
<input type="radio" name="Owner" <?php if($row['Owner']=="B") {echo "checked"}?> value="B">B
Or using the more compact short echo tag <?= .. ?> and ternary operator:
<label>Owner: <?=$row['Owner']?></label></br>
<input type="radio" name="Owner" <?=$row['Owner']=="A" ? "checked" : ""?> value="A">A
<input type="radio" name="Owner" <?=$row['Owner']=="B" ? "checked" : ""?> value="B">B
Note that you need double equals signs for comparisons.
try this code not = but use ==
<input type="radio" name="Owner" <?php if($row['Owner'] == "A") { echo "checked"; }?> value="A">
$gender=$row['gender'];
<input type="radio" name="gender" <?php if($gender=="Male"){?> checked="true" <?php } ?> />Male
<input type="radio" name="gender" <?php if($gender=="Female"){?> checked="true" <?php } ?>/>Female
$owner=$row['owner'];
$owners= ['A'=>'', 'B'=> ''];
$owners[$owner] = 'checked';
<input type="radio" name="Owner" <?php echo $owners['A']?> value="A">A
<input type="radio" name="Owner" <?php echo $owners['B']?> value="B">B
i think it's easy and simple and so useful if you have many values in DB.
i tried the answer given by #trincot but it give me errors so i have make little changes to improve the answer
<input type="radio" name="Owner" <?php if($row['Owner']=="A") {?> <?php echo "checked";?> <?php }?> value="A">A
<input type="radio" name="Owner" <?php if($row['Owner']=="B") {?> <?php echo "checked";?> <?php }?> value="B">B

Checkbox auto checked based on value from database

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

How do I get checkbox as checked if value is stored in database?

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/>

Categories