I need to fetch questions with 4 optional answers from dtabase....
tables are questions | options...html code is below...

<table width="536" height="54">
                             <tr>
                                <td class="deco">Q<?php echo $index.") ".$questions; ?> </td>
                                 <td width="15"></td>
                             </tr>
                             <?php while($row_wrong_ans = mysql_fetch_array($res_wrong_ans)){ 
                                    $op_index = $row_wrong_ans['fbo_index']; 
                                    $op = $row_wrong_ans['fbo_ans'];
                             ?>
                             <tr>
                                <td width="473"><input type="radio" name="radio[<?php $index;?>]" value="<?php echo $op_index;?>"  /><?php echo $op; }?></td>
                                 <td><input type="hidden" name="hid" value="<?php echo $index;?> " /></td><br />
                              </tr>                            
                          </table>
                        <?php } ?>

In the next page I need to get the value of the Radio(having a dynamic name)...with $_POST..how to get it????
Plz help....
Thank you

Dani AI

Generated

You are very close. Two quick fixes in your markup: actually echo the question index in the array-style name, and keep the same name for all 4 options of a question so the browser treats them as one group. Add ids and labels so the text is clickable too.

<input type="radio"
       name="radio[<?php echo (int)$index; ?>]"
       id="q<?php echo (int)$index; ?>_<?php echo (int)$op_index; ?>"
       value="<?php echo (int)$op_index; ?>">
<label for="q<?php echo (int)$index; ?>_<?php echo (int)$op_index; ?>">
  <?php echo htmlspecialchars($op, ENT_QUOTES, 'UTF-8'); ?>
</label>

Radios are grouped by sharing the same name; each should have a unique id that the label references. (developer.mozilla.org)

On submit, PHP will give you an array $_POST['radio'] keyed by the question index you used in name="radio[...]". If the user does not pick an option for a question, that key will be missing because browsers omit unchecked radio groups; and if you forget a value, the posted value becomes the string on. Example processing with basic validation:

$answers = isset($_POST['radio']) && is_array($_POST['radio']) ? $_POST['radio'] : [];
foreach ($answers as $qid => $optId) {
    $qid   = (int)$qid;
    $optId = (int)$optId;
    // TODO: verify $optId actually belongs to $qid in your DB before saving
}

PHP builds arrays from bracketed input names like radio[123], which is why this works. Unchecked radio groups are not submitted, and value should never be omitted. (php.net)

Cleanups: you do not need the hidden hid field; the array key already carries the question id. Use the option’s id as each radio’s value, and consider adding required to one radio in each group to enforce selection client-side. (developer.mozilla.org)

Finally, a modernization tip: your snippet uses mysql_* functions. That extension was deprecated in PHP 5.5 and removed in PHP 7.0; switch to mysqli or PDO. (php.net)

Nice direction from on iterating the posted answers; the tweaks above should make it plug-and-play.

Recommended Answers

All 3 Replies

I would suggest you iterate through the $_POST['radio'] array. E.g.:

// Attempt to retrieve answers
$answers = isset($_POST['radio']) ? $_POST['radio'] : array();

// Iterate through each answer
foreach($answers as $question_id => $answer) {
    echo "Q{$question_id}: {$answer}<br />"; 
}

Thanks a lot...You are mind blowing....thank u so much..:):):)

Need to change a bit in HTML part..m trying myself if not then ll get back to u....thank u so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.