使用下一个按钮替换页面上的内容,而无需将我重定向到新页面

时间:2015-12-30 03:23:53

标签: php mysql button radio-button next

我试图制作一个测验应用程序,用侧面的单选按钮显示答案。当您按下一个按钮时,将出现一组新答案并替换。

当我按下下一个按钮时,我设法按预期弹出四个问题并弹出四个新问题。 现在有一个问题,我的第一组四个答案(qid = 1)不会消失,这很奇怪,因为qid = 2和3的另一组答案确实并且每当我按下一个按钮时替换彼此。

如何制作新答案并替换旧答案?

这是我到目前为止的代码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'>";

1 个答案:

答案 0 :(得分:1)

您需要使用表单标记分隔输入。对于你的每个循环做这样的事情..

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>";

<小时/> 试试这个..

<?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>