1

Following form has repeating input fields. Each field has two hidden inputs and one text input. I want to retrieve the values of each field when posted, so that I can store in the database.

<form action="" method="post">

    <?php
        $arr = [30, 40, 55];        
        foreach ($arr as $code) { ?>
            <?php 
                for ($i = 1; $i <= 12; $i++ ) { ?>
                    <input type="hidden" name="field[code]" value="<?php echo $code; ?>">
                    <input type="hidden" name="field[month]" value="<?php echo $i; ?>">
                    <input type="text" name="field[amount]"><?php 
                } 
        }
    ?>

    <button name="submit">Submit</button>

</form>

I can't figure out how to run the loop to retrieve all three values of each field. I'm trying the following:

<?php
    if( isset( $_POST['submit']) ){

        foreach( $_POST['field'] as $field ) { //<- problem
            $code = $field['code'];
            $month = $field['month'];
            $amount = $field['amount'];

            //insert into db
        } 

    }
?>
1
  • 2
    step 1) var_dump $_POST - see what's actually passed Commented Oct 18, 2019 at 9:07

3 Answers 3

1

Assign a number foreach input field.

<?php
       $arr = [30, 40, 55];
       foreach ($arr as $code) { ?>
           <?php
               for ($i = 1; $i <= 12; $i++ ) { ?>
                   <input type="hidden" name="field[<?php echo $i ?>][code]" value="<?php echo $code; ?>">
                   <input type="hidden" name="field[<?php echo $i ?>][month]" value="<?php echo $i; ?>">
                   <input type="text" name="field[<?php echo $i ?>][amount]"><?php
               }
       }

   ?>

On the back-end

<?php
    if( isset( $_POST['submit']) ){
        foreach( $_POST['field'] as $key => $field ) { 
            $code = $field['code'];
            $month = $field['month'];
            $amount = $field['amount'];
            //insert into db
        }

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

Comments

1

This probably is what you are looking for:

<?php
// ...
if (isset( $_POST['submit']) && array_key_exists('field', $_POST)) {

    $code = $_POST['field']['code'];
    $month = $_POST['field']['month'];
    $amount = $_POST['field']['amount'];

    //insert into db
} 

Or if you consider this easier to read:

<?php
// ...
if (isset( $_POST['submit']) && array_key_exists('field', $_POST)) {

    $field = &$_POST['field'];
    $code = $field['code'];
    $month = $field['month'];
    $amount = $field['amount'];

    //insert into db
} 

Comments

1
<form action="" method="post">

    <?php
        $arr = [30, 40, 55];        
        foreach ($arr as $code) { ?>
            <?php 
                for ($i = 1; $i <= 12; $i++ ) { ?>
                    <input type="hidden" name="field[code][]" value="<?php echo $code; ?>">
                    <input type="hidden" name="field[month][]" value="<?php echo $i; ?>">
                    <input type="text" name="field[amount][]"><?php 
                } 
        }
    ?>

    <button name="submit">Submit</button>

</form>



then,dump($_POST);

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.