0
<?php 
    $question = array();
    $answer   = array();
    for($i=1; $i<=5; $i++) {
?>
<div class="container">
    <form action="" method="post">
        <div class="form-group">
            <label>Question No: <?php echo $i; ?></label>
            <?php echo "<input type='text' class='form-control' name='qu[$i]'><br/>";
            echo "<lable>Answer: </lable>";
            echo "<input type='text' class='form-control' name='ans[$i]'><br>";?>
        </div>
    </form>
</div>
<?php } ?>
<center><button name='submit' class='btn btn-primary btn-home' style="width: 200px;">Submit</button></center>

Here is my code, I want to get all data which is entered in this form. But as a array. After submit the form, this form will collect the values from a loop, so I have to collect values from this loop and convert those to an array.

3
  • 2
    Why do you open $num forms? Keep the form tags outside the loop. And check what you get in $_POST when you submit the form. Commented Nov 24, 2016 at 17:32
  • Sorry, it was my mistake, here $num means 5 Commented Nov 24, 2016 at 17:50
  • nono the $num is fine, but dont open 5 forms, open one. And does your form have an action or is it empty like here? Commented Nov 24, 2016 at 17:51

1 Answer 1

1

I think your idea is fine up to the point where you open a new form every time.

Try something like this:

<div class="container">
  <form action="" method="post">
  <?php 
    $question = array();
    $answer   = array();
    for($i=1; $i<=$num; $i++) {
  ?>
    <div class="form-group">
      <label>Question No: <?php echo $i; ?></label>
      <?php echo "<input type='text' class='form-control' name='qu[$i]'><br/>";
            echo "<lable>Answer: </lable>";
            echo "<input type='text' class='form-control' name='ans[$i]'><br>";?>        
    </div>
  <?php } ?>

    <center><button name='submit' class='btn btn-primary btn-home' style="width: 200px;">Submit</button></center>

  </form>
</div>

And to check if this is what you want just var_dump($_POST); on the page that receives the request.

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

5 Comments

Actually I want to get the values of this from, as an array.
@AzharulIslam Ok, you should get them when you submit the form. What do you get when you submit the form?
Nothing, Because I didn't print anything right now. This is a form which is in a for-loop. And after submit the form, I want to get those values as a array which is entered on this form.
I'm sorry but I'm confused. How are you testing this? When you submit the form the values shall be inside $_POST global on the page targeted by the action attribute.
@AzharulIslam Glad I could help out :)

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.