0

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 ......

Wilest
  • 1,820
  • 7
  • 36
  • 62

2 Answers2

2

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.

Mark
  • 502
  • 4
  • 10
  • I've done what you suggested: 1. Changed to echo "$option1
    "; but now I get an error: Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in ...
    – Wilest Oct 06 '11 at 09:25
  • Sorry meant to wrap in curly braces, have changed above. – Mark Oct 06 '11 at 09:31
  • The curly braces explicitly specify the end of the name of the variable. – Mark Oct 06 '11 at 09:35
  • Thank you Mark, I've changed the form, and also changed the action page to use == instead of = but it's still complaining, I'll keep on trying and report back. – Wilest Oct 06 '11 at 09:38
  • You will also have to change the `foreach` statement as your post names are no longer the question array they are 'question_1','question_2' – Mark Oct 06 '11 at 09:42
  • sorry, I've changed my foreach to the following but I still get the same error. Any ideas? foreach($_POST['question_{$q_nr}[]'] as $key => $ans) { if ($ans == '') { echo "answer is empty"; } } – Wilest Oct 06 '11 at 10:00
1

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.

M. Suleiman
  • 858
  • 4
  • 22
  • Thank you, I used print_r($_POST); and get the output: Array ( [question_1] => A ) and then underneath it: Warning: Invalid argument supplied for foreach() – Wilest Oct 06 '11 at 10:22
  • try `if( is_array( $_POST['question'] ) ) { foreach ( $_POST['question'] as $key=>$value ) { ... } else { echo "$_POST['question'] isn't an array"; }` and think of variable variables – M. Suleiman Oct 06 '11 at 10:28
  • wait a minute... How come $_POST['question'] is an array in your case? in your case $_POST IS an array, but not $_POST['question'] – M. Suleiman Oct 06 '11 at 10:36
  • OK you have put me in the right direction here. If I only use $_POST in my foreach I don't get the error any more, but my if statement still gives no output to validate the form. Sigh. – Wilest Oct 06 '11 at 12:16
  • Still wanted to add: `foreach($_POST as $key => $ans) { echo $key; echo $ans; if (empty($_POST)) { echo "empty"; } else { echo "not empty"; } }` – Wilest Oct 06 '11 at 12:18
  • That's because $ans in this case is not an array. Try `$ans` instead of `$ans[]` out and tell me what you got :) – M. Suleiman Oct 06 '11 at 12:54
  • what I've managed to do now is actually get a results. I think what is happening it is not doing the foreach because no answers was selected in the test. If I move the 'if(empt($_POST)) to in front of the foreach, I get a result. How am I going to check if each question was answered if it is not doing the foreach? – Wilest Oct 06 '11 at 13:06