I have a table name courses
courses('course_id','course_name','course_desc')
I am using php. I have 2 rows of record in this table. And I have used a while
loop to list the two as radio button options. I want to add a hidden input field that posts the related course_id
to the course selected in the radio button. What I have right now is.
while($row = mysql_fetch_assoc($result)){
echo '<td><input type="hidden" name="courseid" value="'.$row['course_id'].'"><strong class="panel-title">'.$row['coursename'].'</strong> - '.$row['course_desc'].'<br><input type="radio" name="'.$row['coursename'].'" value="'.$row['coursename'].'"> - Choose '.$row['coursename'].'</td></tr>';
The related hidden input wont get selected when I select 1 of the course. How to achieve that. I hope it would be possible without use of jquery as much as possible.
You should put that course_id
in value of radio field, so that you will get it posted with your form and by that course_id
you can easily retrieve course_name
while($row = mysql_fetch_assoc($result)){
echo '<td>'.
'<strong class="panel-title">'.$row['coursename'].'</strong> - '.
$row['course_desc'].'<br><input type="radio" name="'.$row['coursename'].'" value="'.$row['course_id'].'"> '.
'- Choose '.$row['course_name']
.'</td>';
OR
If you want to have course_name
as well then you can change your value to something like this
... value="'.$row['course_id'].'-'.$row['course_name'].'" ...
and then you can easily break it to get course_id
and course_name
in PHP or JavaScript
I think for a radio button to work properly, all the name
attribute must be same. If not you will be able to select two or more radio buttons.