0

I have a form with groups of inputs with same names but different values:

<form method="POST">
    <div class="move_menu_item_inputs">
        <input type="text" name="menu_item_name" value="Pizza">
        <input type="text" name="plus_minus_qty" value="1">
    </div>

    <div class="move_menu_item_inputs">
        <input type="text" name="menu_item_name" value="Sandwich">
        <input type="text" name="plus_minus_qty" value="3">
    </div>
</form>

At this moment I can concatenate one input values of latest group of inputs

So... this

$concatenate = $_POST['plus_minus_qty'] . "x" . $_POST['menu_item_name'];

will have as result:

$concatenate = 3x Sandwich

I need to get "1x Pizza, 3x Sandwich" in my variable! Anyone can help please?

2 Answers 2

4

You cannot get that result unless you change the names of the variables, because the second couple of controls will always trump the first one as long as they share names.

The simplest solution would be to append [] to all the names, which would make the corresponding values inside $_POST be arrays. Then you could do something like:

$stuff = array_combine($_POST['menu_item_name'], $_POST['plus_minus_qty']);
$descriptions = [];
foreach ($stuff as $ingredient => $quantity) {
    $descriptions[] = $quantity."x ".$ingredient;
}

$concatenate = implode(', ', $descriptions);
Sign up to request clarification or add additional context in comments.

Comments

0

Php post var array needs unique key names.

I would make the names the values. So its like this:

<div class="move_menu_item_inputs">

    <input label="pizza" type="text" name="pizza" value="?">
</div>

<div class="move_menu_item_inputs">
    <input label="donuts" type="text" name="donuts" value="?">
</div>

For creating the concatenation part, its something like this:

$concat = '';
foreach( $_POST as $name => value )
{
    $concat += $name . "=" $value;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.