0
votes

I want to $_POST the ID of the selected radio button. The problem is the value of my radio button is not the ID, but the string title.

foreach($products as $product) {
    echo '<input required type="radio" id="'.$product->ID.'" name="product" value="'.$product->post_title.'">';
    echo '<label for="'.$product->ID.'">'.$product->post_title.'</label><br>'; 
    echo '<input type="hidden" name="postid" value="'.$product->ID.'">';   
} 

Is there a way I can "match" that hidden field, so I can access the proper value in $_POST? If I just do $_POST["postid"] it does not work because it just gets the last record from the foreach loop

You shouldn't have duplicate IDs. Don't use the same ID for the radio button and the hidden input. - Barmar
Thanks. Let me remove it! - Rollor
Why don't you just change the radio button to have the product ID in the value? then you don't need the hidden input. - Barmar
The hidden input shouldn't be in the loop. You only need one of them to hold the ID of the selected radio button. You'll need to use JavaScript to do that. - Barmar
Because I'm required to particularly insert the string post_title to the db field - Rollor