gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:48:40 25 4
gpt4 key购买 nike

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

我成功地按预期弹出了四个问题,并在按下“下一步”按钮时弹出了四个新问题。现在有一个问题,我的第一组四个答案(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'>";

最佳答案

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

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>

关于php - 使用下一个按钮替换页面上的内容,而不将我重定向到新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34522501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com