Emerald
Emerald

Reputation: 409

Get the hidden input of radio button

I have a radio button that fetch and display from database using a foreach loop which is holding a string value. I set another hidden input along with the radio button which is holding the id of each data.

$sql = "SELECT DISTINCT(class) FROM program WHERE level = '".$level."' AND title = '".$title."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $i = 1;
    while($row = $result->fetch_assoc()) {
        echo '<ul>';
        echo '<li>';
        echo $row['class'];
        echo '<input type="hidden" value="'.$row['class'].'"';
        echo '</li>';
        echo '<li>';
        echo '<ul><li><input name="timeslot['.$i.']" type="radio" value="" checked>N/A</li></ul>';

        $sql2 = 'SELECT * FROM program WHERE class = "'.$row['class'].'"';
        $result2 = $conn->query($sql2);

        if ($result->num_rows > 0) {
            while($row2 = $result2->fetch_assoc()) {
                echo '<ul>';
                echo '<li>';
                echo '<input name="timeslot['.$i.']" type="radio" value="'.$row['class'].', '.$row2['timeslot'].'" />'.$row2['timeslot'];
                echo '<input type="hidden" name="slot_id['.$i.']" value="'.$row2['id'].'"';
                echo '</li>';
                echo '</ul>';
            }
        }

        echo '</li>';
        echo '</ul>';
        $i++;
    }
}

When submitting the form, my hidden value always getting the last row for each group. How do I get the id value upon the checked radio button?

Upvotes: 0

Views: 518

Answers (1)

nithinTa
nithinTa

Reputation: 1642

You are not closed input hidden field.

Also, I would suggest changing this way instead of radio and a hidden if you dont need every id in server.

echo '<input name="timeslot['.$row2['id'].']" type="radio" value="'.$row['class'].', '.$row2['timeslot'].'" />'.$row2['timeslot'];

Upvotes: 0

Related Questions