ArmorCode
ArmorCode

Reputation: 749

How to reference 2d array of dynamic textboxes in php

I have created a 2d array of textboxes like this:

<form name="form1" Method="POST" ACTION="dynamic_process.php">

        <?php for( $i = 1; $i <= $userinput; $i++ ) 
        { 
            echo '(X'.$i.' '; ?>
            <input id="txtbox" name="txtbox[][x]"  type="text" />
            <?php echo ', Y'.$i.' '; ?>
            <input id="txtbox" name="txtbox[][y]"  type="text" />
            <?php echo ', Z'.$i.' '; ?>
            <input id="txtbox" name="txtbox[][z]"  type="text" />
        <?php echo ')<br>'; 
        } ?>    
        <br>
    <input type="hidden" name="MaxCoordinates" value="<?php echo $userinput?>">
    <input type="submit" name="submit_coordinates" value="submit" />
</form>

I'd like to cycle through the xyz coordinates first, and print them out on the the dynamic_process.php page. I am having a very difficult time understanding how to reference the indexes in the array.

The first index of the textbox[] is dynamically decided by user input, the second index, (the one with the x, y, or z in it) is not decided by user input.

one of my attempts at referencing the textboxes from dynamic_process.php looks like this.

<?php
foreach ( $_POST['txtbox[]'] as $txtbox )
{
    echo '<br>';
    foreach ( txtbox[][] as $point)

        echo '  '.$txtbox[]['X']. ', ';
        echo '  '.$txtbox[]['Y']. ', ';
        echo '  '.$txtbox[]['Z']. ', ';
    }
}?>

but I get this error on the page when it loads:

Fatal error: Cannot use [] for reading in C:\myfolder\dynamic_process.php on line 21

Line 21 is this:

foreach ( txtbox[][] as $point)

Upvotes: 0

Views: 112

Answers (2)

TimBrownlaw
TimBrownlaw

Reputation: 5507

There are many issues here, Too many to go over, so the following is just a "suggested" attempt at fixing this. This has been "tested" as I was debugging it.

NOTE: I have renamed some of your variables for readability (as an example) but everyone has their own preferences.

<form name="form1" method="POST" action="dynamic_processt.php">
    <?php
    // Added for testing as this is not specified in the supplied code
    $user_input = 2; 
    // If you are going to use ID's, each id has to be unique,
    //   so I have just come up with "something" to make them different.
    // Why do you even need id's?
    for ($i = 1; $i <= $user_input; $i ++) {
        // This can be refactored a lot as it is not beautiful.
        echo '(X' . $i . ' '; ?>
        <input id="text_box_1_<?= $i;?>" name="text_box[<?= $i;?>][x]" type="text"/>
        <?php echo ', Y' . $i . ' '; ?>
        <input id="text_box_2_<?= $i;?>" name="text_box[<?= $i;?>][y]" type="text"/>
        <?php echo ', Z' . $i . ' '; ?>
        <input id="text_box_3_<?= $i;?>" name="text_box[<?= $i;?>][z]" type="text"/>
        <?php echo ')<br>';
    } ?>
    <br>
    <input type="hidden" name="MaxCoordinates" value="<?php echo $user_input ?>">
    <input type="submit" name="submit_coordinates" value="submit"/>
</form>
<?php

To display the results: You have a 2D array, so the First foreach will give you access to the 1st level elements.

Then you reference the array element directly with your named x,y,z indexes. When you use the var_dump($_POST) it will show you the actual structure of your arrays.

Note that in the $_POST for instance, you have an array but you need to reference it by just the variable name.

// Debug
var_dump($_POST); // What does the structure of $_POST Look like?

foreach ($_POST['text_box'] as $text_box) {
    echo '<br>';
    echo '  ' . $text_box['x'] . ', ';
    echo '  ' . $text_box['y'] . ', ';
    echo '  ' . $text_box['z'] . ', ';

} ?>

There are a ton of things you can do to make this nicer, but I'll leave that to you.

The use of var_dump() is very very handy in seeing what is going on, which in this case is very different to what you are expecting. Always make sure you know what your variables are doing!

Ask if you have any questions.

Upvotes: 1

Evinn
Evinn

Reputation: 153

when you get post submit data, i think you should access it by

<?php
foreach ( $_POST['txtbox'] as $txtbox )
{

}?>

or maybe it could be better if in the html you not make it become txtbox[][x] but become txtbox[$i][x] it will become more readable

good luck

Upvotes: 0

Related Questions