Sorry if this is a really simple question, I am just learning php, but I've been writing a script that can create quizzes, and right now html hates me. When I click a radio button and submit my form the result is always that the button is on, not the value I assign it. Here is the excerpt of my code:
echo '<form action="index.php" method="get">';
for ($i=0; $i<$_SESSION["fsize"];$i++)
{
echo 'Type of Answer for Question ' . $i + 1 . ": <br> ";
echo '<input type="radio" name="f' . $i . ' value="radio">Radio Buttons <br>';
echo '<input type="radio" name="f' . $i . ' value="text">Text area <br>';
echo '<input type="radio" name="f' . $i . ' value="Checkboxes">Checkboxes <br>';
The html of my page, which is very incomplete, generated by my php is this:
<html>
<body>
<form action="index.php" method="post">
Number of questions:
<input type="number" name="fsize">
<input type="submit">
</form>
<form action="index.php" method="get">
1:
<br>
<input type="radio" name="f0 value="radio">
Radio Buttons
<br>
<input type="radio" name="f0 value="text">
Text area
<br>
<input type="radio" name="f0 value="Checkboxes">
Checkboxes
<br>
1:
<br>
<input type="radio" name="f1 value="radio">
Radio Buttons
<br>
<input type="radio" name="f1 value="text">
Text area
<br>
<input type="radio" name="f1 value="Checkboxes">
Checkboxes
<br>
1:
<br>
<input type="radio" name="f2 value="radio">
Radio Buttons
<br>
<input type="radio" name="f2 value="text">
Text area
<br>
<input type="radio" name="f2 value="Checkboxes">
Checkboxes
<br>
<input type="submit">
\n
</form>
</body>
</html>
I specifically switched to get rather than post so I could see my results, and this is the important part of my url /index.php?f0+value%3D=on&f1+value%3D=on&f2+value%3D=on
Does anyone know how I can get it to submit properly? and if you want to see the full php script here it is:
<html>
<body>
<?php
session_start();
echo '<form action="index.php" method="post"> Number of questions: <input type="number"
name="fsize">';
echo '<input type="submit"></form>';
if (isset($_POST["fsize"]))
{
$_SESSION["fsize"] = $_POST["fsize"];
}
if (isset($_SESSION["fsize"]))
{
echo '<form action="index.php" method="get">';
for ($i=0; $i<$_SESSION["fsize"];$i++)
{
echo 'Type of Answer for Question ' . $i + 1 . ": <br> ";
echo '<input type="radio" name="f' . $i . ' value="radio">Radio Buttons <br>';
echo '<input type="radio" name="f' . $i . ' value="text">Text area <br>';
echo '<input type="radio" name="f' . $i . ' value="Checkboxes">Checkboxes <br>';
if (isset($_GET["f" . $i]))
{
$_SESSION["f".$i] = $_GET["f".$i];
echo "I exist!";
}
if (isset($_SESSION["f".$i]))
{
echo '<textarea rows="4" cols="50"> What is the question? </textarea>';
switch($_SESSION["f".$i])
{
case("radio"):
echo "you chose a radio button!";
break;
case("text"):
echo "You chose a text area!";
break;
case("Checkboxes"):
echo "You chose checkboxes!";
break;
default:
break;
}
}
else if (isset($_GET["f".$i]))
{
echo '<textarea rows="4" cols="50"> What is the question? </textarea>';
switch($_GET["f".$i])
{
case("radio"):
echo "you chose a radio button!";
break;
case("text"):
echo "You chose a text area!";
break;
case("Checkboxes"):
echo "You chose checkboxes!";
break;
default:
break;
}
}
}
echo '<input type="submit"> </form>';
}
?>
</body>
</html>
I have a few other problems with it too I know, but this is the most infuriating. Sorry about the length, Hovestar.
You've forgot a closing " in each name attribute of your radio controls:
<input type="radio" name="f0" value="radio">
<!-- ^ this here is important -->
Therefore, your browser thinks this radio's name attribute is "f0 value="
and is missing a value attribute. Therefore it adds the default attribute on
.
Try this.
echo '<form action="index.php" method="get">';
for ($i=0; $i<$_SESSION["fsize"];$i++)
{
echo 'Type of Answer for Question ' . $i + 1 . ": <br> ";
echo '<input type="radio" name="f' . $i . '" value="radio">Radio Buttons <br>';
echo '<input type="radio" name="f' . $i . '" value="text">Text area <br>';
echo '<input type="radio" name="f' . $i . '" value="Checkboxes">Checkboxes <br>
if you have more statements make sure to close it properly using "
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With