David Brett
David Brett

Reputation: 59

POST multiple values with same input names

I'm trying to POST multiple values using a hidden input. But the problem is now that If I post one value all the inputs will be posted. The problem is that the inputs are dynamically created in the foreach all with the same name....

DATABASE CONNECTION .. .. .. ... AND QUERY

foreach ($key as $value){
    echo "<input type='hidden' name='create[]' 
    value=" .$value['NumberID'] .">

   <input type='submit' name='store'>"
    }

    if(isset($_POST['create'])){
      $NumberID = $_POST['create'];
      print_r($NumberID);
    }

The php creates 5 input types. But the hidden input value are all unique by the numberID. So my questions is there an option to post multiple values one by one.

Someone told me about a simple for loop? But I don't get that cause I already foreach it..?

I hope to hear from you soon guys and appreciate your help :)

Upvotes: 0

Views: 567

Answers (2)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

You don't need a hidden input to do this. You can use a <button> instead of an <input> and pass the ID as its value.

<?php foreach ($key as $value): ?>
    <button type="submit" name="create" value="<?= $value['NumberID'] ?>">
        store
    </button>
<?php endforeach; ?>

$_POST['create'] will be the NumberID of the button you clicked.

Upvotes: 0

Dave S
Dave S

Reputation: 1502

How about using a counter?

$n = 1;
foreach ($key as $value){
echo "<input type='hidden' name='create$n' value=" .$value['NumberID'] .">"
...
$n++:
}

or are you string to create one name='create' with value=' (val1), (val2), (val3) ... ' ?

If so, your foreach should build that 1 concat-string then after the foreach do a single echo of name and value = concat-string.

Upvotes: 1

Related Questions