chirayu patil chirayu patil - 1 year ago 86
HTML Question

How do I set values for radio buttons in PHP?

How do I set names for each radio button in the following PHP code using counter variable, or any other alternative?


$id=0; //counter variable

$sql ="select roll_number,student_name,gender from student_table";
$query=mysqli_query($dbcon, $sql) or die(mysqli_error($dbcon));


echo '<tr>';
echo '<td>'. $post['roll_number'] . '</td>';
echo '<td>'. $post['student_name'] . '</td>';
echo '<td>'. $post['gender'] . '</td>';
echo '<td width=250>';

echo '<input type="radio" name="rad'.$id'" value="P" />Present';
echo '<input type="radio" name="rad'.$id'" value="A" />Absent';

echo '<button type="submit" name="save" value="Save" class="btn btn-success btn-sm">Save</button>';
echo '</td>';
echo '</tr>';



Answer Source
echo '<input type="radio" name="rad[' . $post['roll_number'] . ']" value="P" />Present';
echo '<input type="radio" name="rad[' . $post['roll_number'] . ']" value="A" />Absent';

On the receiving end you may do like this:

$answers = $_POST['rad'];
foreach($answers as $roll_number=>$answer){
    //do something with the $roll_number and $answer...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download