PHP Scrub PHP Scrub - 4 years ago 79
PHP Question

Using the next button to replace content on my page, without redirecting me to a new page

I'm trying to make a quiz application that shows answers with radiobuttons 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'>";

Answer Source

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>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download