2

Not sure if the question title is clear enough - but what I was referring to is - is it a bad practice to give a numeric value say 102 to the name attribute of a radio button like

<input type="radio" name="102" value="2032" /> Hello World

If it is a bad idea - the how do I handle this situation. I have a multiple choice exam where all 60 questions are displayed on the page at once.

The questions are stored in a question array and for each question - the multiple choices are stored in an answer array

This is the for loop which generates the 60 questions with their multiple choice answers - right now I'm using question_id as the value for radio buttons

<?php
$question_ids = array_keys($this->question_array);
foreach($question_ids as $question_id) 
{
    $question = $this->question_array[$question_id];
    $answers = $this->answer_array[$question_id];
?>
    <div> 
        <strong>Q) <?php echo $question['question'];?></strong>         
    <div>
        <?php foreach ($answers as $answer) { ?><br/>
        <input type="radio" name="<?php echo $question_id;?>" value="<?php echo $answer['answer_id'];?>" /> <?php echo $answer['answer'];?>
        <?php } ?>          
    </div>
    </div>
<?php 
} ?>

So I have assigned name with question_id and value with answer_id - so on server side - I will know for which question which answer option was selected - needless to say they are unique autogenerated id's from the database

foreach ($_POST as $key => $value)
    echo "For question id ".htmlspecialchars($key)." answer selected is ".htmlspecialchars($value)."<br>";

If assigning number is a bad idea - why is that. The other option for me is to assign a name value like name="questionId-102" and on the server side parse it.

Let me know what is the best way to handle this. Thanks

2 Answers 2

2

A better practice would be to put it in an array of questions:

<?php foreach ($answers as $answer) { ?><br/>
<input type="radio" name="questions[<?=$question_id;?>]" value="<?=$answer['answer_id'];?>" /> <?=$answer['answer'];?>

And:

foreach ($_POST['questions'] as $key => $value)
    echo "For question id ".htmlspecialchars($key)." answer selected is ".htmlspecialchars($value)."<br>";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - much cleaner option too
0

You can use a simple string before the name in case you want a series or sequence of numbers. It is not advisable to use number as name for an element. Try using the following code

<?php
$question_ids = array_keys($this->question_array);
foreach($question_ids as $question_id) 
{
    $question = $this->question_array[$question_id];
    $answers = $this->answer_array[$question_id];
?>
    <div> 
        <strong>Q) <?php echo $question['question'];?></strong>         
    <div>
        <?php foreach ($answers as $answer) { ?><br/>
        <input type="radio" name="question_<?php echo $question_id;?>" value="<?php echo $answer['answer_id'];?>" /> <?php echo $answer['answer'];?>
        <?php } ?>          
    </div>
    </div>
<?php 
} ?>

You can then use explode() function to get the question id for further process by using '_' as delimiter and second element of the array as question id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.