Displaying Data From Database Not Working Properly In PHP/javascript
I have a survey form displaying questions, answertype and number of options from my question table. Here's my code.
Solution 1:
1) Id attribute
should be unique
for each element.
2) You have much code redundant
.reduce the code redundant using for
loop .
3) Remove your unwanted display:none
style property and jquery . because you can able to handle the empty option in server side code .
PHP : Simplified version of your code
$result = mysqli_query($con,$query);
if($result)
{
while($row = mysqli_fetch_array($result))
{
$questions = $row['questiontitle'];
$qid = $row['question_id'];
$anstype = $row['answer_type'];
$count++;
$answer++;
echo "<b>Question ". $count.".)</b> ". $questions. "</br>";
$option1 = $row['Option_1'];
$option2 = $row['Option_2'];
$option3 = $row['Option_3'];
$option4 = $row['Option_4'];
$option5 = $row['Option_5'];
$option6 = $row['Option_6'];
$option7 = $row['Option_7'];
$option8 = $row['Option_8'];
$option9 = $row['Option_9'];
$option10 = $row['Option_10'];
?>
<input style="display:none" type="text" class="form-control" value="<?php echo $qid ?>" name="question_<?php echo $answer; ?>">
<?php
for($j=1;j<=10;$j++)
{
if(isset(${'Option_'.$j}) && !empty(${'Option_'.$j}) )
{
?>
<div>
<input type="<?php if($anstype == 'radiobutton'){ echo 'radio'; }else{ echo 'checkbox'; }?>" name="answer_<?php echo $answer ?>" value="<?php echo ${'Option_'.$j};?>" id="<?php echo ${'Option_'.$j}; ?>"><?php echo ${'Option_'.$j}; ?>
</div>
<?php
}
?>
<br/>
<?php
}
?>
<?php
}
}
?>
Post a Comment for "Displaying Data From Database Not Working Properly In PHP/javascript"