The Sumo
The Sumo

Reputation: 637

Php $_POST form with multiple variable inputs in foreach loop

I have a form which is creating several text input's in a foreach loop like this:

    <?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'] ) ){

$entry_id = sanitize_text_field($_POST['entry_id']);
$position = sanitize_text_field($_POST['position']);
$points = sanitize_text_field($_POST['points']);

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

}
?>

The result is an html form which looks something like this:

<form action="" method="post">
    <p>Aiden Hopkins
      <input type="hidden" name="entry_id" value="98">
      <input type="text" name="position" value="">
      <input type="text" name="points" value="">
    </p>
    <p>Asha Sykes
      <input type="hidden" name="entry_id" value="75">
      <input type="text" name="position" value="">
      <input type="text" name="points" value="">
    </p>
    <p>Ffion Hughes
      <input type="hidden" name="entry_id" value="66">
      <input type="text" name="position" value="">
      <input type="text" name="points" value="">
  <input type="submit" name="submit" value="Save Changes" class="button">
</form>

The problem I have is that when I try and submit the form and write the results for each user to the database, it only enters the details for the last instance of the loop and ignores the others.

I have looked at other answers on here such as How to loop through an array of inputs in a form? but when I tried that, I got 'Array' for all the db entries.

I'm sure there is a simple way to do this using foreach or something but I'm just getting muddled up with it and I must confess, array's are not my strong point!

Upvotes: 2

Views: 8111

Answers (2)

BizzyBob
BizzyBob

Reputation: 14740

As @Anonymous mentioned, your problem is that you are using the same names for your input. You should use unique names.

You can name your inputs with [] to indicate an array of values.

ex:

<input type="text" name="array[]" value=1>
<input type="text" name="array[]" value=2>
<input type="text" name="array[]" value=3> 

Posting the above inputs would give you:

$_POST['array'][0] = 1
$_POST['array'][1] = 2
$_POST['array'][2] = 3

In your case, you may find it helpful to have the data grouped by id like:

[
    98 => [
        'position' => 'value',
        'points' => 0
    ],
    75 => [
        'position' => 'value1',
        'points' => 11
    ],
    66 => [
        'position' => 'value2',
        'points' => 22
    ],
]

in which case your PHP would look like:

<?php foreach ($results as $entry) { ?>
    <p>
        <?php echo $entry_name;?>
        <input type="text" name="person[$entry->user_id][position]" value="">
        <input type="text" name="person[$entry->user_id][points]" value="">             
    </p>
<?php    }?>

Upvotes: 0

LP154
LP154

Reputation: 1497

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,
           )
        );

    }
}
?>

Upvotes: 3

Related Questions