Foreach Value from Post from Form

Foreach value from POST from form

Use array-like fields:

<input name="name_for_the_items[]"/>

You can loop through the fields:

foreach($_POST['name_for_the_items'] as $item)
{
//do something with $item
}

submit form data from a foreach loop

foreach ($_POST['name'] as $val) { /* do what you want, want you want with my value */ }

$_POST['name'] is just an array. Use count or any array function you want on it then.

Php $_POST form with multiple variable inputs in foreach loop

If you want to submit multiple inputs with the same name, you have to declare it as an array : <input type="hidden" name="entry_id[]" value="<?=$entry->user_id; ?>">. Then, you can iterate on your POST :

<?php $results = $wpdb->get_results( "SELECT * FROM wp_results WHERE event_id = '$event' AND div_id = '$division'" ); ?>
<form action="" method="post">
<?php foreach ($results as $entry) { ?>
<p>
<?php echo $entry_name;?>
<input type="hidden" name="entry_id[]" value="<?=$entry->user_id; ?>">
<input type="text" name="position[]" value="">
<input type="text" name="points[]" value="">
</p>
<?php }?>
<input type="submit" name="submit" value="Save Changes" class="button">
</form>
<?php
if ( isset( $_POST['submit'] ) ){
// Iterate over POST values
foreach ($_POST['entry_id'] as $key => $val) {
$entry_id = sanitize_text_field($_POST['entry_id'][$key]);
$position = sanitize_text_field($_POST['position'][$key]);
$points = sanitize_text_field($_POST['points'][$key]);

$wpdb->update('wp_results',
array(
'position' => $position,
'points' => $points,
),
array(
'user_id' => $entry_id,
)
);

}
}
?>

How can I retrieve _POST data from a form created with a foreach loop?

In your code above, you tried to create dynamic variables for each of the comment. That's not how dynamic variables can be created. You already have the data in POST request in array form so you can access each comment by the key you have created in the form comment concatenated with operationCode number. Try this!

foreach($operationCodeName as $operation => $year) {
// replace space in operation key with _
$operation = preg_replace('/\s+/', '_', $operation);
$commentKey = "comment" . $operation;
$comment = $_POST[$commentKey];
echo "<tr>
<td>" . $operation . "</td>
<td align='center'>" . $year . "</td>";
echo "<td>" . $comment . "</td>
</tr>";
}

form action inside foreach works only for the first value of php

So, I was able to solve this issue, and the solution is the "id" so when you are using foreach, for every iteration, it is re-creating the form with the different value of the input, of course!

But the problem is the id-field, even if the input value is different, but the id is same for every iteration and therefore, even if you click a different button i.e. the 2nd or 3rd, the POST request will consider the initial value as the actual value, i.e. the first iteration, just because the id was same!

It's similar to the concept of first-come-first-serve. Since the POST request getting the value of the first element correspondent to the id, therefore that will the final value of that specific id and no amount how many times you iterate, the value will not be updated if you use the same id.

The possible fix is to have a different id, with each different iteration and that was made possible with putting the foreach value in the id.

<?php
foreach ($res as $r) {
?>
<form id="clearitem<?php echo $r['cart_id'];?>" action="clearitem.php" method="POST" enctype="multipart/form-data">
<input type="text" name="clearitem" value="<?php echo $r['cart_id'];?>">
<button type="Submit">×</button>
</form>
<?php
}
?>

Array foreach to form display and get the same result

You are writing your HTML code incorrectly, if you want to have indexed values:

echo $key.' <input name="$key[]" type="text" value="$value">';

This line is parsing to, ex, name="status[]" which is not what you want.

Instead, modify the lines to read as:

<?php
foreach ($datas as $k => $data) {
echo $data['name'];
foreach ($data as $key => $value) {
echo $key.' <input name="$k[$key]" type="text" value="$value">';
}
}
?>

Now this parses as name="data1[status]"



Related Topics



Leave a reply



Submit