I have a list where people can answer questions, each question has three possible answers using radio buttons.
Inserting that data in my database works fine but now I want to be able to edit those lists so I need to display an already filled in list from my database instead of inserting a new one.
Normal data is no problem but I am having some issues with the radio buttons.
My database structure looks like this:
wpi_info
-id
-other non important fields for this question
wpi_categories
- id
- title
- info_id (same as id of wpi_info)
wpi_questions
-id
-title
-answer
-cid (same as id of wpi_categories)
This is how I currently show the radio buttons (categories/questions show up but the radio buttons are not checked):
<?PHP
$een = 1;
$twee = 2;
$drie = 3;
$getcats = 'SELECT * FROM wpi_categories WHERE info_id = "'.$conn->real_escape_string($getinfo['id']).'" ORDER BY id';
$getcatscon = $conn->query($getcats);
while($getcats = $getcatscon->fetch_assoc()){
if(!empty($getcats['title'])){
$werkplekinspectie .= '
<label class="categorytitle">'.$getcats['title'].'</label>
<div class="row">';
$getquestions = 'SELECT * from wpi_questions WHERE cid = "'.$getcats['id'].'"';
$getquestionscon = $conn->query($getquestions);
while($getquestions = $getquestionscon->fetch_assoc()){
$werkplekinspectie .= '
<div class="col-md-8">
<p class="questionclass">'.$getquestions['title'].'</p>
</div>
<div class="col-md-4">
<div class="container text-right">
<input type="radio" name="questionlist['.$getcats['title'].']['.$getquestions['title'].']" id="radio-'.$een.'" value="ok" required>
<label class="radiotoggle" for="radio-'.$een.'"><span class="radio">Ok</span></label>
<input type="radio" name="questionlist['.$getcats['title'].']['.$getquestions['title'].']" id="radio-'.$twee.'" value="fout">
<label class="radiotoggle" for="radio-'.$twee.'"><span class="radio">Fout</span></label>
<input type="radio" name="questionlist['.$getcats['title'].']['.$getquestions['title'].']" id="radio-'.$drie.'" value="nvt">
<label class="radiotoggle" for="radio-'.$drie.'"><span class="radio">N.v.t</span></label>
</div>
</div>';
$een+=3;
$twee+=3;
$drie+=3;
}
$werkplekinspectie .= '
</div>';
}
}
echo $werkplekinspectie;
?>
The values ok
, nvt
, fout
are what is stored in the answer
column of wpi_questions
.
For every radio button, if the answer matches the value I need it to be checked. How can I do that?
You can do it in jquery by checking the value from database and keeping it as checked by
$('#id').prop('checked', true);
enter code here use following code snippet as example. I have set male as default you can put a female in the gender variable
<?php $gender ='male';?>
<input type="radio" name="gender" value="male" <?php echo isset($gender) && !empty($gender) && $gender == 'male' ? 'checked' :'' ; ?> > Male<br>
<input type="radio" name="gender" value="female" <?php echo isset($gender) && !empty($gender) && $gender == 'female' ? 'checked' :'' ; ?> > Female<br>
You can simply check if the answer matches and output a checked
attribute:
'...
<input type="radio" name="questionlist['.$getcats['title'].']['.$getquestions['title'].']" id="radio-'.$een.'" value="ok" required ' . ($getquestions['answer'] == 'ok' ?'checked':'') . '>
<label class="radiotoggle" for="radio-'.$een.'"><span class="radio">Ok</span></label>
<input type="radio" name="questionlist['.$getcats['title'].']['.$getquestions['title'].']" id="radio-'.$twee.'" value="fout" ' . ($getquestions['answer'] == 'fout'?'checked':'') . '>
<label class="radiotoggle" for="radio-'.$twee.'"><span class="radio">Fout</span></label>
<input type="radio" name="questionlist['.$getcats['title'].']['.$getquestions['title'].']" id="radio-'.$drie.'" value="nvt" ' . ($getquestions['answer'] == 'nvt'?'checked':'') . '>
<label class="radiotoggle" for="radio-'.$drie.'"><span class="radio">N.v.t</span></label>
...'
however the whole code block is very hard to read, and will certainly develop bugs over time.
It is widely accepted that mixing data access logic with html is a terrible idea, but if you are stuck maintaining a huge old legacy app, it might not be feasible to refactor completely
In that case, you can at least make your code a lot easier to maintain by placing the data access at the top, and html generation underneath, utilizing phps templating syntax instead of huge string concatenations.