An effective way to display a survey one application at a time on one web page

So, I am new to HTML / CSS and web development, and I am developing a website for Likert's online survey. At first I had a website, so when a participant clicked on the link, the entire survey will be displayed on the page with a submit button at the bottom of the page. However, my professor wants it to be designed in such a way that only one survey application is displayed at a time, and the user can click Previous or Next to navigate the survey.

I wrote code that does this, but it seems incredibly inefficient to me. There must be a better way to do what I'm trying to accomplish!

Here is an example of the approach I took:

<html>
<body>
<script type="text/javascript">
function showNext(divIdShow,divIdHide){
    document.getElementById(divIdShow).style.display="block";
    document.getElementById(divIdHide).style.display="none";
    }
function showPrevious(divIdShow,divIdHide){
    document.getElementById(divIdShow).style.display="block";
    document.getElementById(divIdHide).style.display="none";
    }
</script>

    <div id="div1" class="center">
    <table align="center" style="font-size: 14px"  bgcolor="CCCCCC">
    <tr><td>A survey statement goes here<td></tr></table>
    <table align="center" style="font-size: 14px"  bgcolor="CCCCCC">
        <tr><td><input type="radio" name="s1" value="1">Strongly Disagree  (1)</td>
            <td><input type="radio" name="s1" value="2">Disagree (2)</td>
            <td><input type="radio" name="s1" value="3">Neutral (3)</td>
            <td><input type="radio" name="s1" value="4">Agree (4)</td>
            <td><input type="radio" name="s1" value="5">Strongly Agree (5)</td>
            <td><input type="radio" name="s1" value="0" checked><i>not answered</i></td>
        </tr>
    </table><br>
    <button type="button" disabled>Previous</button>
    <button type="button" onclick="showNext('div2','div1')">Next</button>
    </div>

    <div id="div2" class="center" style="display:none">
    <table align="center" id="t1" style="font-size: 14px"  bgcolor="CCCCCC">
    <tr>Another statement goes here!</tr>
        <tr><td><input type="radio" name="s2" value="1">Strongly Disagree  (1)</td>
            <td><input type="radio" name="s2" value="2">Disagree (2)</td>
            <td><input type="radio" name="s2" value="3">Neutral (3)</td>
            <td><input type="radio" name="s2" value="4">Agree (4)</td>
            <td><input type="radio" name="s2" value="5">Strongly Agree (5)</td>
            <td><input type="radio" name="s2" value="0" checked><i>not answered</i></td>
        </tr>
    </table><br>
    <button type="button" onclick="showPrevious('div1','div2')">Previous</button>
    <button type="button" onclick="showNext('div2','div1')">Next</button>
    </div>
</body>
</html>

, . , .

!

**** **** , looping/echo php. ( ) , :

<html>
<body>

<script type="text/javascript">
function go(){
document.getElementById('state0').style.display = "block";
document.getElementById('state0').scrollIntoView();
}
function showNext(divIdShow,divIdHide){
    document.getElementById(divIdShow).style.display="block";

    document.getElementById(divIdHide).style.display="none";
    }
function showPrevious(divIdShow,divIdHide){
    document.getElementById(divIdShow).style.display="block";

    document.getElementById(divIdHide).style.display="none";
    }
</script>


<button type='button' onclick='go()'>GO</button>

<!--<button type='button' onclick="go()">go</button>-->
    <?php
    $numElements=5;
    $array = array("This is the first statement", "This is the second statement","This is the third statement","This is the fourth Statement", "Last statement here");

    for($i=0;$i<$numElements;$i++){

    $divId='state'.$i; 
    $sId='q'.$i;
    $nextInt=$i+1;
    $prevInt=$i-1;
    $nextDiv='state'.$nextInt;
    $curDiv='state'.$i;
    $prevDiv='state'.$prevInt;
    if($i==0){
    echo "<div id='$divId' class='center' style='display:none'>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
    <tr><td>$array[$i]<td></tr></table>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
        <tr><td><input type='radio' name='$sId'  value='1'>Strongly Disagree  (1)</td>
            <td><input type='radio' name='$sId'  value='2'>Disagree  (2)</td>
            <td><input type='radio' name='$sId'  value='3'>Neutral  (3)</td>
            <td><input type='radio' name='$sId'  value='4'>Agree  (4)</td>
            <td><input type='radio' name='$sId'  value='5'>Strongly Agree  (5)</td>
            <td><input type='radio' name='$sId'  value='6'>No answer  </td>
        </tr>
    </table><br>
    <button type='button' disabled>Previous</button>
    <button type='button' onclick='showNext(\"$nextDiv\",\"$curDiv\")'>Next</button>
    </div>";}
    else if($i==$numElements-1){
    echo "<div id='$divId' class='center' style='display:none'>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
    <tr><td>$array[$i]<td></tr></table>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
        <tr><td><input type='radio' name='$sId'  value='1'>Strongly Disagree  (1)</td>
            <td><input type='radio' name='$sId'  value='2'>Disagree  (2)</td>
            <td><input type='radio' name='$sId'  value='3'>Neutral  (3)</td>
            <td><input type='radio' name='$sId'  value='4'>Agree  (4)</td>
            <td><input type='radio' name='$sId'  value='5'>Strongly Agree  (5)</td>
            <td><input type='radio' name='$sId'  value='6'>No answer  </td>
        </tr>
    </table><br>
    <button type='button' onclick='showPrevious(\"$prevDiv\",\"$curDiv\")'>Previous</button>
    <button type='button'  disabled>Next</button>
    </div>";}
    else{
    echo "<div id='$divId' class='center' style='display:none'>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
    <tr><td>$array[$i]<td></tr></table>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
        <tr><td><input type='radio' name='$sId'  value='1'>Strongly Disagree  (1)</td>
            <td><input type='radio' name='$sId'  value='2'>Disagree  (2)</td>
            <td><input type='radio' name='$sId'  value='3'>Neutral  (3)</td>
            <td><input type='radio' name='$sId'  value='4'>Agree  (4)</td>
            <td><input type='radio' name='$sId'  value='5'>Strongly Agree  (5)</td>
            <td><input type='radio' name='$sId'  value='6'>No answer  </td>
        </tr>
    </table><br>
    <button type='button' onclick='showPrevious(\"$prevDiv\",\"$curDiv\")'>Previous</button>
    <button type='button' onclick='showNext(\"$nextDiv\",\"$curDiv\")'>Next</button>
    </div>";    }

    }


    ?>

</body>
</html>

, !

+4
3

, :

json - (, = [[1, " xy?" ], [2, "2- ],...], , . prev/next , .
Fiddle, , .

var questions = [[1, "how are you"],[2, "next one"],[3, "question number 3"]];

setFirstQuestion();

function setFirstQuestion(){
  $(".question").text(questions[0][1]);
  $(".question").data("current",[0][0]);
}

$("#next").click(function(){
   setNextQuestion();
});

function setNextQuestion()
{
  var current = $(".question").data("current");
  $(".question").text(questions[current + 1][1]);
  $(".question").data("current", current + 1);
}

, 50 . , , .

+1

. .

function MakeQuestion(Q) {
    var lq = document.createElement('div');
    /* ... */
    return lq;
}

var Questions = [ { q: "Ham?", a: ['yes', 'double-yes', 'super-yes', 'bacon too'] }, ... ];

Questions.map(function(Q) {
    document.getElementById('qContainer').appendChild( MakeQuestion(Q) );
} );
+1
<html>
<body>

<script type="text/javascript">
function go(){
document.getElementById('state0').style.display = "block";
document.getElementById('state0').scrollIntoView();
}
function showNext(divIdShow,divIdHide){
    document.getElementById(divIdShow).style.display="block";

    document.getElementById(divIdHide).style.display="none";
    }
function showPrevious(divIdShow,divIdHide){
    document.getElementById(divIdShow).style.display="block";

    document.getElementById(divIdHide).style.display="none";
    }
</script>


<button type='button' onclick='go()'>GO</button>

<!--<button type='button' onclick="go()">go</button>-->
    <?php
    $numElements=5;
    $array = array("This is the first statement", "This is the second statement","This is the third statement","This is the fourth Statement", "Last statement here");

    for($i=0;$i<$numElements;$i++){

    $divId='state'.$i; 
    $sId='q'.$i;
    $nextInt=$i+1;
    $prevInt=$i-1;
    $nextDiv='state'.$nextInt;
    $curDiv='state'.$i;
    $prevDiv='state'.$prevInt;
    if($i==0){
    echo "<div id='$divId' class='center' style='display:none'>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
    <tr><td>$array[$i]<td></tr></table>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
        <tr><td><input type='radio' name='$sId'  value='1'>Strongly Disagree  (1)</td>
            <td><input type='radio' name='$sId'  value='2'>Disagree  (2)</td>
            <td><input type='radio' name='$sId'  value='3'>Neutral  (3)</td>
            <td><input type='radio' name='$sId'  value='4'>Agree  (4)</td>
            <td><input type='radio' name='$sId'  value='5'>Strongly Agree  (5)</td>
            <td><input type='radio' name='$sId'  value='6'>No answer  </td>
        </tr>
    </table><br>
    <button type='button' disabled>Previous</button>
    <button type='button' onclick='showNext(\"$nextDiv\",\"$curDiv\")'>Next</button>
    </div>";}
    else if($i==$numElements-1){
    echo "<div id='$divId' class='center' style='display:none'>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
    <tr><td>$array[$i]<td></tr></table>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
        <tr><td><input type='radio' name='$sId'  value='1'>Strongly Disagree  (1)</td>
            <td><input type='radio' name='$sId'  value='2'>Disagree  (2)</td>
            <td><input type='radio' name='$sId'  value='3'>Neutral  (3)</td>
            <td><input type='radio' name='$sId'  value='4'>Agree  (4)</td>
            <td><input type='radio' name='$sId'  value='5'>Strongly Agree  (5)</td>
            <td><input type='radio' name='$sId'  value='6'>No answer  </td>
        </tr>
    </table><br>
    <button type='button' onclick='showPrevious(\"$prevDiv\",\"$curDiv\")'>Previous</button>
    <button type='button'  disabled>Next</button>
    </div>";}
    else{
    echo "<div id='$divId' class='center' style='display:none'>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
    <tr><td>$array[$i]<td></tr></table>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
        <tr><td><input type='radio' name='$sId'  value='1'>Strongly Disagree  (1)</td>
            <td><input type='radio' name='$sId'  value='2'>Disagree  (2)</td>
            <td><input type='radio' name='$sId'  value='3'>Neutral  (3)</td>
            <td><input type='radio' name='$sId'  value='4'>Agree  (4)</td>
            <td><input type='radio' name='$sId'  value='5'>Strongly Agree  (5)</td>
            <td><input type='radio' name='$sId'  value='6'>No answer  </td>
        </tr>
    </table><br>
    <button type='button' onclick='showPrevious(\"$prevDiv\",\"$curDiv\")'>Previous</button>
    <button type='button' onclick='showNext(\"$nextDiv\",\"$curDiv\")'>Next</button>
    </div>";    }

    }


    ?>

</body>
</html>

Again, here is the answer

+1
source

All Articles