4

I'm trying to send multiples values to a PHP form. This is the form that I use to send values:

<form id="form1" name="form1" method="POST" action="../WSweb/Factura.php">
    <table width="561" height="79" border="1">
        <tr>
            <td width="30%" height="32">ProductID</td>
            <td width="30%" height="32">SKU</td>
        </tr>
        <?php do { ?>
            <tr>
                <td><?php echo $row_test1['ProductID']; ?>
                    <input
                        type="hidden"
                        name="Product[id][ProductID]"
                        value="<?php echo $row_test1['ProductID']; ?>"
                    />
                </td>
                <td><?php echo $row_test1['SKU']; ?>
                    <input
                        type="hidden"
                        name="Product[id][SKU]"
                        value="<?php echo $row_test1['SKU']; ?>"
                    />
                </td>
            </tr>
        <?php } while ($row_test1 = mysqli_fetch_assoc($test1)); ?>
    </table>

    <input type="submit" value="Update" name="Facturar">
</form>

And this is the action file:

if(isset($_POST['Update']))
{
    $ProductID=$_POST['Product'];
    print_r(json_encode($ProductID));
}

The problem that I have is when I send multiple values, for example the below table:

ProductID      SKU
103              WH004BI
137              VO007BI

I alway get this result:

{"id":{"ProductID":"137","SKU":"VO007BI"}}

When I actually want to get a result like this:

{"id":[{"ProductID":"103","SKU":"WH004BI"},{"ProductID":"137","SKU":"VO007BI"}]}
4
  • Change name="Product[id][ProductID]" to name="Product[id][][ProductID]". HTML can have arrays too! This is the same as if you were appending an element to array using the [] syntax. Do this for all of the named inputs. Commented May 8, 2019 at 16:21
  • i just doing but i get this ProductID SKU 103 WH004BI 137 VO007BI its any form to get this? {"id":[{"ProductID":"103","SKU":"WH004BI"},{"ProductID":"137","SKU":"VO007BI"}]} Commented May 8, 2019 at 16:34
  • I don't think you can get that structure from a form directly, you'll need to rearrange the data in PHP. Commented May 8, 2019 at 16:36
  • i exect somenthing like that {"id":[{"ProductID":"103","SKU":"WH004BI"},{"ProductID":"137","SKU":"VO007BI"}]} Commented May 8, 2019 at 17:12

1 Answer 1

2

You're going to want to do something like this:

<form id="form1" name="form1" method="POST" action="../WSweb/Factura.php">
    <table width="561" height="79" border="1">
        <tr>
            <td width="30%" height="32">ProductID</td>
            <td width="30%" height="32">SKU</td>
        </tr>
        <?php $i = 0; ?>
        <?php while ($row_test1 = mysqli_fetch_assoc($test1) { ?>
            <tr>
                <td>
                    <?php echo $row_test1['ProductID']; ?>
                    <input
                        type="hidden"
                        name="Product[id][<?= $i; ?>][ProductID]"
                        value="<?php echo $row_test1['ProductID']; ?>"
                    />
                </td>
                <td>
                    <?php echo $row_test1['SKU']; ?>
                    <input
                        type="hidden"
                        name="Product[id][<?= $i; ?>][SKU]"
                        value="<?php echo $row_test1['SKU']; ?>"
                    />
                </td>
            </tr>
        <?php $i++; ?>
        <?php } ?>
    </table>

    <input type="submit" value="Update" name="Facturar">
</form>

Note that I have put a $i = 0 at the start of the loop, and $i++ at the end of the loop.

Additionally, I have changed the names to the following:

name="Product[id][<?= $i; ?>][SKU]"

Which will prevent the issue you were having in the comment section regarding an ill-formed array.

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

2 Comments

i just doing but i get this result {"id":[{"ProductID":"137","SKU":"VO007BI"]} but i done get all the array
@JavierJoasimarPalosFlores Please see my code. I have tested this in my own environment and everything appears to be working correctly. Please make sure you copy the code exactly as it is in the answer.

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.