0

I'm looping a question for the user with different numerical values for $i. They will input a quantity in the html form that is also looped. Upon them clicking submit, I would like to have an array (with the key ranging form [0] onwards) that stores their response to each particular variant of the question. However, with the code I have written, I only manage to store their last input with the key [0] as if it was the first element of the array. All the previous answers seem to be lost when I call print_r. Please, I would really appreciate it if anyone could point out why this is happening or how I could fix it.

<?php

    for ($i=2; $i<=10; $i++) 

    {
        print "question $i";

        echo"<form action=\"mysqlranked.php\" method=\"post\">
          <input type=\"text\" name=\"pools[]\" value=\"0\" maxlength=\"2\" size=\"2\">
          </form>
          <br>";
    }

    print "
    <form>
    <input type=\"submit\" name=\"formSubmit\" value=\"Submit\">
    </form>";

    if (isset($_POST["formSubmit"]))
    {
        $var = $_POST["pools"];
    }

    print_r($var);

?>
3
  • For the sake of everyone involved, can you please switch to using single quotes (apostrophes) so you can drop all those incessant escape slashes? Commented Jan 24, 2012 at 5:52
  • 1
    @rockerest even better, stop echo-ing HTML from PHP (see Moon's answer) Commented Jan 24, 2012 at 5:55
  • @Phil, I'd normally agree, but for this case it does seem a bit easier to use single quotes than to have to keep typing <?PHP ?> or even <? ?> Commented Jan 24, 2012 at 5:57

4 Answers 4

1

You had each of your inputs in a new form and the submit button in its own form as well. I fixed it for you:

<?php

    echo "<form action=\"mysqlranked.php\" method=\"post\">";
    for ($i=2; $i<=10; $i++) 

    {
        print "question $i";

        echo"
          <input type=\"text\" name=\"pools[]\" value=\"0\" maxlength=\"2\" size=\"2\">

          <br>";
    }

    print "
    <input type=\"submit\" name=\"formSubmit\" value=\"Submit\">
    ";
    echo "</form>";
    if (isset($_POST["formSubmit"]))
    {
        $var = $_POST["pools"];
    }

    print_r($var);

?>

Let me know if it works.

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

2 Comments

You should check $_POST['pools'] before attempting to use it.
@gf3, No problem. Also take into account the various suggestions floating around in the comments. Use single quotes when echoing html strings so you don't have to escape your double quotes, check the value of $_POST['pools'] before using it, and possibly use actual html instead of echoing strings.
1

You should not have a new form tag for every question.

Try the code below:

<form action="mysqlranked.php" method="post">

<?php
    for ($i = 2; $i <= 10; $i++)
    {
        print "question $i";
?>

    <input type="text" name="pools[]" value="0" maxlength="2" size="2">

<?php
    }
?>

    <input type="submit" name="formSubmit" value="Submit">
</form>

<?php
    if (isset($_POST["formSubmit"]))
    {
        $var = $_POST["pools"];
    }

    print_r($var);
?>

Comments

0
    <?php

        for ($i=2; $i<=10; $i++) 

        {
            print "question $i";

            echo"<form action=\"mysqlranked.php\" method=\"post\">
              <input type=\"text\" name=\"pools[]\" value=\"0\" maxlength=\"2\" size=\"2\">

              <br>";
        }

        print "

        <input type=\"submit\" name=\"formSubmit\" value=\"Submit\">
        </form>";

     if (isset($_POST["formSubmit"])) 
        {

                for($counter=0;$counter<count($_POST["pools"]);$counter++)
                {
                    $var = $_POST['pools'][$counter]

                }

         }
print_r($var);

    ?>

Comments

0

you may use array_push($arrayName, $elementToBePushed);

Comments

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.