使用下一个按钮替换页面上的内容,而无需将我重定向到新页面
I'm trying to make a quiz application that shows answers with radio buttons on the sides. When you press the next button a set of new answers will appear and replace the .
I've managed to make four questions pop up as intended and four new ones popup when I press the next button. Right now theres one problem, my first set of four answers (with qid = 1) does not dissapear, which is weird since the other set of answers with qid = 2 and 3 does and replace eachother whenever I press the next button.
How do I make it so that the new answers appear and replace the old answers?
Here is my code so far PHP:
$qid1 = 1;
$sql1 = mysqli_query($connect,"SELECT * FROM question where qid ='$qid1'");
while($row=mysqli_fetch_assoc($sql1))
{
echo "<input type='radio' name='answer1' value='".$row['Point']."'>"
.$row['answer'] ."<br>";
}
echo "<input type='submit' name='forward1' value='next'>";
$qid2 = 2;
$sql2 = mysqli_query($connect,"SELECT * FROM question where qid ='$qid2'");
while($row2=mysqli_fetch_assoc($sql2)){
if (isset($_POST['forward1'])) {
echo "<input type='radio' name='answer2' value='".$row2['Point']."'>"
.$row2['answer'] ."<br>";
}
}
echo "<input type='submit' name='forward2' value='next'>";
$qid3 = 3;
$sql3 = mysqli_query($connect,"SELECT * FROM question where qid ='$qid3'");
while($row3=mysqli_fetch_assoc($sql3)){
if (isset($_POST['forward2'])) {
echo "<input type='radio' name='answer3' value='".$row3['Point']."'>"
.$row3['answer'] ."<br>";
}
}
echo "<input type='submit' name='forward3' value='next'>";
我正在尝试制作一个测验应用程序,用两侧的单选按钮显示答案。 当你按下一个按钮时,会出现一组新的答案并替换。 p>
我设法按预期弹出四个问题,当我按下一个时弹出四个新问题 按钮。 现在有一个问题,我的第一组四个答案(qid = 1)不会消失,这很奇怪,因为qid = 2和3的另一组答案确实并且每当我按下一个按钮时替换另一个 。 p>
如何制作新答案并替换旧答案? p>
这是我的代码到目前为止PHP: p>
$ qid1 = 1;
$ sql1 = mysqli_query($ connect,“SELECT * FROM question where qid ='$ qid1'”);
而($ row = mysqli_fetch_assoc($ sql1))
{
echo“&lt; input type ='radio'name ='answer1'value ='”。$ row ['Point']。“'&gt;”
。 $ row ['answer']。“&lt; br&gt;”;
}
echo“&lt; input type ='submit'name ='forward1'value ='next'&gt;”;
$ qid2 = 2;
$ sql2 = mysqli_query($ connect,“SELECT * FROM question where qid ='$ qid2'”);
而($ row2 = mysq li_fetch_assoc($ sql2)){
if(isset($ _ POST ['forward1'])){
echo“&lt; input type ='radio'name ='answer2'value ='”。$ row2 [ 'Point']。“'&gt;”
。$ row2 ['answer']。“&lt; br&gt;”;
}
}
echo“&lt; input type ='submit'name ='forward2'value ='next'&gt;“;
$ qid3 = 3;
$ sql3 = mysqli_query($ connect,”SELECT * FROM question where qid ='$ qid3'“);
\ nwhile($ row3 = mysqli_fetch_assoc($ sql3)){
if(isset($ _ POST ['forward2'])){
echo“&lt; input type ='radio'name ='answer3'value =' “。$ row3 ['Point']。”'&gt;“
。$ row3 ['answer']。”&lt; br&gt;“;
}
}
echo”&lt; input type =' 提交'name ='forward3'值='下一个'&gt;“;
code> pre>
div>
You need to separate your inputs with form tags. For each of your loops do something like this..
echo "<form>";
$sql3 = mysqli_query($connect,"SELECT * FROM question where qid ='$qid3'");
while($row3=mysqli_fetch_assoc($sql3)){
if (isset($_POST['forward2'])) {
echo "<input type='radio' name='answer3' value='".$row3['Point']."'>"
.$row3['answer'] ."<br>";
}
}
echo "<input type='submit' name='forward3' value='next'>";
echo "</form>";
Try this..
<?php
$localhost = "localhost";
$username = "root";
$password = "";
$connect = mysqli_connect($localhost, $username, $password) || die("Kunde inte koppla");
mysqli_select_db($connect, 'wildfire');
// let's put the qid in a session var
session_start();
$qid = isset($_SESSION['qid']) ? $_SESSION['qid']+1 : 1;
$_SESSION['qid'] = $qid;
ob_start();
echo "<form>";
$sql1 = mysqli_query($connect,"SELECT * FROM question where qid ='$qid'");
while($row1=mysqli_fetch_assoc($sql1))
echo "<input type='radio' name='answer1' value='{$row1['Point']}'>{$row1['answer']}<br>";
echo "<input type='submit' name='forward1' value='next'>";
echo "</form>";
$output = ob_get_clean();
?>
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php echo $output; ?>
</body>
</html>