How to Set the Radio Button Based on the Value Fetched from the Database

check a radio button based on value retrieved from database

$Share = 'Yes';
$Permission = 'Yes';
<label for="Do you provide consent to share images on our official web page">
Do you provide consent to share images on our official web page? :
</label>
<input type="radio" name="share" <?php echo ($Share =='Yes')? 'checked':'' ?> value="Yes">Yes<br>
<input type="radio" name="share" <?php echo ($Share =='No')? 'checked':'' ?> value="No">No<br><br>

<label for="If yes, with Identity">If yes, with Identity? :<br><br></label>
<input type="radio" name="permission" <?php echo ($Permission=='Yes')?'checked':'' ?> value="Yes">Yes<br>
<input type="radio" name="permission" <?php echo ($Permission=='No')?'checked':'' ?> value="No">No<br><br>

make radio button checked if it has the right database value

You can change your code to below:

 public function getstatusradio () {
$sql = "SELECT ticketstatus FROM ticketpractice WHERE id = 6";
$result = $this->connect()->query($sql);
if ($result-> rowCount() > 0) {
while ($row = $result->fetch()){
$newCheck = $row['ticketstatus']=='new' ? 'checked' : '';
$processingCheck = $row['ticketstatus']=='processing' ? 'checked' : '';
echo "<input type='radio' name='status' $newCheck value='new '>new";
echo "<input type='radio' name='status' $processingCheck value='processing'>processing";
}
}
}

but if you want to use php tag inside "", use htmlspecialchars()

$phpCode = '<? if($condtion){ ?>';
echo htmlspecialchars($phpCode);

Laravel set radio button is checked based on database value

1) NO need two name attribute.

2) you can use {{ ($site->is_active=="0")? "checked" : "" }} to checked based on database value .

<input type="radio"  id="option1" name="status" value="0"  {{ ($site->is_active=="0")? "checked" : "" }} >OFF</label>

<input type="radio" id="option2" name="status" value="1" {{ ($site->is_active=="1")? "checked" : "" }} >ON</label>

3) for select box do like this

<selec name="xx" >
<option value="1" {{ ($site->select_values=="1")? "selected" : "" }} ></option>
.
.
</select>

Controller :

$site->update([
'name'=>$request['name'],
'copyright'=> $request['copyright'],
'is_active'=>$request['status'], // == 'true' ? 1 : 0
'message'=>$request['message'],
'datatime'=>date('Y-m-d', strtotime($request['datatime'])),
'url'=>$request['url'],
'metadata'=>$request['metadata']

]);

Select which radio button is checked based from database value Django

To set which radio button is selected without using a Django form, you can apply some logic to each radio button which sets the checked attribute. Note that you should also ensure that the value attribute is unique for each element:

<th><input type="radio" name="requirements_completed_{{req.pk}}" value="no"{% if not req.completed %} checked{% endif %}> No
<input type="radio" name="requirements_completed_{{req.pk}}" value="yes"{% if req.completed %} checked{% endif %}> Yes</th>


Related Topics



Leave a reply



Submit