0

I have a list of radio buttons for guests to select whether they're attending an event or not.

A party can have several members, so multiple yes/no options can exist on the same page.

Idea is that a person can go down the list and hit yes or no on guest attendance. I am then trying to segment out the names of those who have accepted and declined into arrays, so I can push it to wpdb.

I have the following that generated the radio options:

<?php
foreach ($results as $result) { 
    $party = $result['party']; // "John, Doe" in DB varchar 
    $party_guests = explode(",", $party);

    $attending_array = array();
    $not_attending_array = array();
  
    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_array[<?php echo $index; ?>]" type="radio" name="attending-<?php echo $index; ?>" value="<?php echo $guest; ?>" required/>
                <label for="attending-yes-<?php echo $index; ?>">Yes</label>
            </div>
            <div class="guest__options-group">
                <input class="guest__attendance-input" id="attending_array[<?php echo $index; ?>]" type="radio"  name="attending-<?php echo $index; ?>" value="<?php echo $guest; ?>" required/>
                <label for="attending-no-<?php echo $index; ?>">No</label>
            </div>
        </div>
        <?php
    }
}
?>

But I cannot see a way in which I can (via PHP) dynamically add the names to the attending_array or not_ attending_array?

How can I do an array_push based on what option the user clicks on? I cannot target a specific name because $party may contain 2 guest, 6 guest etc.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Freddy
  • 683
  • 4
  • 35
  • 114
  • Looks like you have your `id` and `name` naming convention reversed. Because `for` and `id` are related, you will want to use integer-suffixes for those. Because you want to submit an array of attendance selections, you want to use array syntax with the `name` attributes. Within a normal form submission, only the fields with `name` attributes will be in the payload and they will be keyed by the `name` attribute. Shall we close this question as a typo question? You know what to do, you just accidentally did it in reverse. – mickmackusa Dec 06 '22 at 21:24
  • Of course, you cannot populate those two php variables with user selections because PHP is finished running before the user will even see the output. You'll need to use javascript or a form submission to receive those selections and then you can populate those PHP variables. Alternatively, you can populate javascript variables using the user's selections (without triggering a server-side process). It seems you have a fundamental flaw in your understanding of client-side/server-side processes. – mickmackusa Dec 06 '22 at 21:34

0 Answers0