devlv21
devlv21

Reputation: 25

How can I get the total count of the selected radio button value

After I click on submit how can I get the count of of how many "Yes" were picked or how many "No" were Picked using PHP

         while($row = mysqli_fetch_array($result)) 
                                   {
                                       $i++;
                                   ?>
    
    <p><?php echo $row["question"] ; ?></p>
    
        <input type="radio" name="<?php echo $row["question"].$i ; ?>"  value="Yes">
        <label for="Yes">Yes</label>
        <input type="radio" name="<?php echo $row["question"].$i ; ?>"  value="No">
        <label for="Bo">No</label>
     
        <?php 
                                }
        ?>
    </div><br>
    
    <input type="submit" name="submit"  value="Submit">
    </div>
    </form>
<?php
if(isset($_POST['submit'])){

//count
}

Upvotes: 1

Views: 79

Answers (1)

Kinglish
Kinglish

Reputation: 23654

put in some kind of identifier you can pick up in the $_POST, like

    <input type="radio" name="question_radio_<?php echo $row["question"].$i ; ?>"  value="Yes">
    <label for="Yes">Yes</label>
    <input type="radio" name="question_radio_<?php echo $row["question"].$i ; ?>"  value="No">
    <label for="Bo">No</label>

Then in php

$s = 'question_radio_';
$yes = 0;
foreach ($_POST as $p => $n) {
 if (strpos($p, $s) !=== false && $n==='Yes') $yes ++; 
}

Upvotes: 2

Related Questions