Reputation: 59
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
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
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