php – How to get data (not value or name) from radio options to POST to database

Question

I have a set of options that are generated based on database count. For example, if a group has 4 members, 4 radio option pairs of yes and no will show:

Name 1

 - Attending? Yes / No

Name 2

 - Attending? Yes / No

etc

I’m trying to then get all the names of the guests who have clicked “yes”, they are attending.

I can’t do $_POST['radio_name'] as all radio pairings have different name‘s and the value of all of them are set to “yes”.

For example, here are the fields for 2 guests:

<?php foreach ($results as $result){ 

  $party = $result['party']; // "John, Doe" in DB varchar 
  $party_guests = explode(",", $party);

  foreach ($party_guests as $index=>$guest){ ?>

  <span class="guest__name"><?php echo $guest; ?></span>
  <div class="guest__options-option">
    <span>Attending?</span>
    <div class="guest__options-group">
      <input class="guest__attendance-input" id="attending-yes-<?php echo $index; ?>" type="radio" name="attending-<?php echo $index; ?>" value="yes" required/>
      <label for="attending-yes-<?php echo $index; ?>">Yes</label>
    </div>
    <div class="guest__options-group">
      <input class="guest__attendance-input" id="attending-no-<?php echo $index; ?>" type="radio"  name="attending-<?php echo $index; ?>" value="no" required/>
      <label for="attending-no-<?php echo $index; ?>">No</label>
    </div>
  </div>

  <?php }
  
} ?>

As mentioned, I’m trying to collate a list of all those who had said yes, and no, into individual variables so I can then push them into the database (which is a varchar, the names will be separated by commas).

However, I’m unsure how to approach this as $_POST[''] will not work dynamically (i.e. $_POST['attending-yes-4'] will not work if there’s only 2 guests generated).

0
Freddy 2 months 2022-12-05T19:20:44-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse