Skip to content Skip to sidebar Skip to footer

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'];

         ?>


         <inputstyle="display:none"type="text"class="form-control"value="<?phpecho$qid?>"name="question_<?phpecho$answer; ?>"><?phpfor($j=1;j<=10;$j++)
        {

             if(isset(${'Option_'.$j}) && !empty(${'Option_'.$j}) )
             { 
             ?><div><inputtype="<?phpif($anstype == 'radiobutton'){  echo'radio'; }else{ echo'checkbox';  }?>"name="answer_<?phpecho$answer?>"value="<?phpecho ${'Option_'.$j};?>"id="<?phpecho ${'Option_'.$j}; ?>"><?phpecho ${'Option_'.$j};  ?></div><?php  
             }
             ?><br/><?php 
        } 
        ?><?php  

    }

}  

?>

Post a Comment for "Displaying Data From Database Not Working Properly In Php/javascript"