在php中验证表单
I'm trying to validate a form of a test. I get an error in answer.php Basically I want to validate that each question has been answered.
The form:
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY q_nr";
$result1=mysql_query($sql1);
echo "<form method='post' name='form1' action='answer.php'>";
while($row1 = mysql_fetch_array($result1))
{
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];
$option3=$row1['option3'];
echo "<P><strong>$q_nr $question</strong><BR>";
echo "<BR>";
echo "</p>";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question[$q_nr]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='radio' name='question[$q_nr]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='radio' name='question[$q_nr]' value='C'>$option3<BR>";
} else {
echo ''; }
} else { // else if not <> mr
if($option1!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='C'>$option3<BR>";
} else {
echo ''; }
} //end else if q_type <> mr
echo "<BR>";
echo "</p>";
} //end while row1
echo "<input type='submit' value='Submit' name='Submit'>";
echo "</form>";
answer.php
foreach($_POST['question'] as $key => $ans) {
if ($ans[] = '') {
echo "answer is empty";
}
}
I get the error: Warning: Invalid argument supplied for foreach() in ......
我正在尝试验证一种形式的测试。 我在answer.php中收到错误。基本上我想确认每个问题都已得到解答。 p>
表格: strong> p>
answer.php strong> p>
我收到错误:警告:为......中的foreach()提供的参数无效 strong> p>
div> $ sql1 =“SELECT * FROM ex_question WHERE test_name ='$ tid'ORDER BY q_nr”;
$ result1 = mysql_query($ sql1);
echo“&lt; form method ='post'name =' form1'action ='answer.php'&gt;“;
while($ row1 = mysql_fetch_array($ result1))
{
$ q_nr = $ row1 ['q_nr'];
$ q_type = $ row1 [' q_type'];
$ question = $ row1 ['question'];
$ option1 = $ row1 ['option1'];
$ option2 = $ row1 ['option2'];
$ option3 = $ row1 ['option3'];
echo“&lt; P&gt;&lt; strong&gt; $ q_nr $ question&lt; / strong&gt;&lt; BR&gt;”;
echo“&lt; BR&gt;”;
echo“&lt; / p&gt;”;
if($ q_type!='mr'){
if($ option1!=“”){
echo“&lt; input type ='radio'name ='question [$ q_nr]'value ='A'&gt; $ option1&lt; BR&gt;“;
} else {
echo''; }
if($ option2!=“”){
echo“&lt; input type ='radio'name ='question [$ q_nr]'value ='B'&gt; $ option2&lt; BR&gt;”;
} else {
echo''; }
if($ option3!=“”){
echo“&lt; input type ='radio'name ='question [$ q_nr]'value ='C'&gt; $ option3&lt; BR&gt;”;
} else {
echo''; }
} else {//否则如果不是&lt;&gt; mr
if($ option1!=“”){
echo“&lt; input type ='checkbox'name ='question [$ q_nr] []'value ='A'&gt; $ option1&lt; BR&gt;”;
} else {
echo''; }
if($ option2!=“”){
echo“&lt; input type ='checkbox'name ='question [$ q_nr] []'value ='B'&gt; $ option2&lt; BR&gt;”;
} else {
echo''; }
if($ option3!=“”){
echo“&lt; input type ='checkbox'name ='question [$ q_nr] []'value ='C'&gt; $ option3&lt; BR&gt;”;
} else {
echo''; }
} //如果q_type&lt;&gt;则结束其他 mr
echo“&lt; BR&gt;”;
echo“&lt; / p&gt;”;
} //结束时row1
echo“&lt; input type ='submit'value ='Submit'name ='提交 '&gt;“;
echo”&lt; / form&gt;“;
code> pre>
foreach($ _ POST ['question'] as $ key =&gt; $ ans){
if($ ans [] =''){
echo“answer is empty”;
}
}
code> pre>
This is probably because your $_POST['question']
is empty. This is what happens when you try to do this with an empty array.
Whereas your HTML says: name='question[$q_nr]'
.
Print the values in the array to see what it contains, use print_r
.
Edit: $_POST['question']
IS NOT an array! While $_POST
IS an array...
Maybe you should try to do something like this:
foreach ($_POST as $key => $value)
Or do it however you want the result to be displayed.
One thing is that you are assigning the answer rather than checking it, use ==
foreach($_POST as $key => $ans) {
if ($ans == '') {
echo "answer is empty";
}
}
and instead of using
name='question[$q_nr]'
I would use for the radio fields
name='question_{$q_nr}'
and for the checkboxes
name='question_{$q_nr}[]'
On answer.php you should be able to do a print_r($_POST) to check what you are getting.