'How do I add multiple time to a form with a button?
I'm trying to create a create quiz page in php with a form that starts off with 1 question and a button that every time it gets clicked it adds another question at the bottom of the form. I'm trying to add the question html into the button, but when the page open, its starts off by echoing the second question as well as the button doesn't work...
echo "<form method='post'></br>";
echo "Question $questionNum: <input type='text' name='q$questionNum' placeholder='Question' required> </br>";
echo "Answer 1: <textarea name='a$questionNum' placeholder='Answer 1' required wrap='hard'></textarea> <input type='checkbox' name='correctA' value='a1$questionNum'></br>";
echo "Answer 2: <textarea name='a$questionNum' placeholder='Answer 2' required></textarea> <input type='checkbox' name='correctA' value='a2$questionNum'></br>";
echo "Answer 3: <textarea name='a$questionNum' placeholder='Answer 3' required></textarea> <input type='checkbox' name='correctA' value='a3$questionNum'></br>";
echo "Answer 4: <textarea name='a$questionNum' placeholder='Answer 4' required></textarea> <input type='checkbox' name='correctA' value='a4$questionNum'></br>";
echo "<input type='button' name='newQuestion' value='New Question'
onclick='$questionNum++;
Question $questionNum: <input type='text' name='q$questionNum' placeholder='Question' required> </br>
Answer 1: <textarea name='a$questionNum' placeholder='Answer 1' required wrap='hard'></textarea> <input type='checkbox' name='correctA' value='a1$questionNum'></br>
Answer 2: <textarea name='a$questionNum' placeholder='Answer 2' required></textarea> <input type='checkbox' name='correctA' value='a2$questionNum'></br>
Answer 3: <textarea name='a$questionNum' placeholder='Answer 3' required></textarea> <input type='checkbox' name='correctA' value='a3$questionNum'></br>
Answer 4: <textarea name='a$questionNum' placeholder='Answer 4' required></textarea> <input type='checkbox' name='correctA' value='a4$questionNum'></br>
</br>";
echo "</form>";
how do I make the button work?
Solution 1:[1]
You can add a division for your second question and set display: none. When you click the button you call a function that changes the display of the division from "display: none" to "display: block"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Elio Bteich |
