0

How would I be able to give the different things different names (as I am creating them in a loop) so that I can use the data? Could I modify name=\"type\" to maybe include $x within the name so that it is different each time?

For($x=0; $x<=$noQuestions-1; $x++){

    echo "<table>";
    echo "<tr>"."Question ".($x+1).": ".$question[$x]."</tr>";
    echo "<form method=\"post\">";
    echo "<Select class=\"form-control\" type=\"text\" name=\"type\" required>";
    echo "<option value=\"1\">".$optionData[$x][0]."</option>";
    echo "<option value=\"2\">".$optionData[$x][1]."</option>";
    echo "<option value=\"3\">".$optionData[$x][2]."</option>";
    echo "<option value=\"4\">".$optionData[$x][3]."</option>";
    echo "</select>";
    echo "</form>";
    echo "</table>";
}
1
  • Did you try? What exactly is the problem? Commented Nov 13, 2016 at 13:10

2 Answers 2

1

You can simply concatenate variable $x in name like this:

echo "<Select class=\"form-control\" type=\"text\" name=\"type".$x."\" required>";

EDIT:

$type = "dropdown";
for($x=0; $x<=$noQuestions-1; $x++)
{
       echo "<table>";
       echo "<tr>"."Question ".($x+1).": ".$question[$x]."</tr>";
       echo "<form method=\"post\">";
       echo "<Select class=\"form-control\" type=\"text\" name=\"".$type.$x."\" required>";
       echo "<option value=\"1\">".$optionData[$x][0]."</option>";
       echo "<option value=\"2\">".$optionData[$x][1]."</option>";
       echo "<option value=\"3\">".$optionData[$x][2]."</option>";
       echo "<option value=\"4\">".$optionData[$x][3]."</option>";
       echo "</select>"; echo "</form>";
       echo "</table>";

 }
Sign up to request clarification or add additional context in comments.

6 Comments

I am only able to get $type0 for some reason when echoing it.
if (isset($_REQUEST['submit'])){ extract($_REQUEST); if ($_POST['dropdown0']=="1") { echo "1"; } elseif ($_POST['dropdown0']=="2") { echo "2"; } elseif ($_POST['dropdown0']=="3") { echo "3"; } elseif ($_POST['dropdown0']=="4") { echo "4"; } if ($_POST['dropdown1']=="1") { echo "1"; } elseif ($_POST['dropdown1']=="2") { echo "2"; } elseif ($_POST['dropdown1']=="3") { echo "3"; } elseif ($_POST['dropdown1']=="4") { echo "4"; } }
What exactly are you trying to do...can you please elaborate?...I tried the answer which I have given, and it's type0, type1, etc are coming.
I am making a multiple choice quiz player. I have already created a test maker so the number of questions in the quiz player depends. So I have a html form within a php loop to create a certain amount of questions and options. Did you echo it in a different way than I did? Could you show me please
I guess you are creating type a variable. If this is the case, then do it like this: $type = "dropdown"; & in place of name: name=\"".$type.$x."\"
|
0

One simple solution would be to add $x in the name like this for example:

echo "< select class=\"form-control\" type=\"text\" name=\"type-$x\" required>";

2 Comments

how would i echo the variable in regular PHP? Would it be "echo $type-0";
It depends on how you submit the fields. If your form method is "post" then you will echo the variable with $_POST ["type-0"], if your form method is "get" you will use $_GET ["type-0"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.