2017-01-24 58 views
0

我的数据库中有一些问题。每页只显示一个问题,并带有“下一步”按钮。当用户点击下一个按钮时,我随机显示数据库中的下一个问题。统计数据库中的行数

所以问题是,当用户到达有最后一个问题,我想改变“下一步”按钮名称为“完成”。

请帮助我如何做到这一点..

this is my count query: 
$nRows = $dbh->query('select count(*) from questions')->fetchColumn(); 
echo $nRows; 

这是我在我的应用程序下一个按钮。

<button id='Next' class="next-button" name="submit_<?php echo $key ?>" value="<?php echo $key ?>" onclick="change_next(this.value)">Next</button> 

这是我的Java脚本代码显示每页一个问题:

<script type="text/javascript"> 

     function change_next(value) 
    { 
    if(value == 0) 
    { 
     var next = value; 
     next++; 

     $('#question_'+value).removeClass('active').addClass('hidden'); 
     $('#question_'+next).removeClass('hidden').addClass('active'); 
    } 
    else 
    { 
     var next = value; 
     next++; 

     $('#question_'+value).removeClass('active').addClass('hidden'); 
     $('#question_'+next).removeClass('hidden').addClass('active'); 
    } 
} 

回答

2

你只需要chagne按钮

var count = 0; 
    function change_next() { 
     var xhttp = new XMLHttpRequest(); 
     xhttp.onreadystatechange = function() { 
     if (this.readyState == 4 && this.status == 200) { 
      document.getElementById("Question").innerHTML = this.responseText; 
      if (count == 5) { 
      //document.getElementById("Next").value="FINISH"; <--sorry this is if you are using <input type="button" not button =/ 
      document.getElementById("Next").innerHTML="FINISH"; 
      } 
      count++; 
     } 
     }; 
     xhttp.open("GET", "Load_Question.php", true); 
     xhttp.send(); 
    } 
+0

我们怎么能在ajax中做同样的事情 – user36

+0

当你做你的调用'change_next' - 将上面的代码添加到ajax调用返回,这将改变按钮名称 – krisph

+0

所以现在当你点击“下一步”按钮,你用PHP随机加载一个问题,但是您希望在不加载页面的情况下通过使用Ajax加载问题来做到这一点? – Nenroz

0
var count = 0; 
function showNextQuestion() { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     count+=1; 
     if(count>=3) { 
     document.getElementById("Next").innerHTML="Finish"; 
     } 
    } 
    }; 
    xhttp.open("GET", "QuestionLoader", true); 
    xhttp.send(); 
} 
0
的价值
<button id='Next' class="next-button" name="submit_<?php echo $key ?>" value="<?php echo $key ?>" onclick="change_next()">Next</button> 

function change_next() 
{ 
    document.getElementById("Next").value="FINISH"; 
}