I have recently read an article that said, that you should never trust user in the sending data.
So let’s say that I have following form inputs:
<label for="input1">Answer 1</label>
<input type="radio" name="question1" value="answer1" id="input1" />
<label for="input2">Answer 2</label>
<input type="radio" name="question1" value="answer2" id="input2" />
<label for="input3">Answer 3</label>
<input type="radio" name="question1" value="answer3" id="input3" />
Is it okay to check whether posted answer is inside aray with answers:
$question1_answers = array("answer1", "answer2", "answer3");
$answer1 = $_POST['question1'];
if ( in_array($answer1, $question1_answers) ) {
echo "OK!";
} else {
echo "Please select proper answer!";
}
Or maybe above code is unnecessary and I should simply read $_POST['question1'] and it will be enough?
Yes! That is very good practice to validate a radio button value like that. The article you read is correct, you should not blindly trust user supplied data.