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

3
  • What do you get if you enter print_r($_POST) in answer.php? Commented Oct 6, 2011 at 9:08
  • Please check my edit in the answer Commented Oct 6, 2011 at 10:39
  • possible duplicate of Invalid argument supplied for foreach() Commented Apr 10, 2014 at 6:33

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I've done what you suggested: 1. Changed to echo "<input type='radio' name='question_$q_nr[]' value='A'>$option1<BR>"; but now I get an error: Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in ...
Sorry meant to wrap in curly braces, have changed above.
The curly braces explicitly specify the end of the name of the variable.
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.
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'
|
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.

7 Comments

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()
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
wait a minute... How come $_POST['question'] is an array in your case? in your case $_POST IS an array, but not $_POST['question']
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.
Still wanted to add: foreach($_POST as $key => $ans) { echo $key; echo $ans; if (empty($_POST)) { echo "empty"; } else { echo "not empty"; } }
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.