Page 1 of 1

Sooo...how do you work with Checkboxes in PhP?

Posted: Wed Aug 10, 2005 10:37 am
by nawhaley
I'm currently working on pulling a data from a form did a few tests and realized my method of pulling data from a checkbox list is ...well..not working. Simply put its never going into the loop to check the values at all.

Code to make the form with checkboxes

Code: Select all

while(odbc_fetch_row($answerresult))
        {
          
          $answer = odbc_result($answerresult,"Answer");
          $correct = odbc_result($answerresult,"Correct");
          print('<INPUT TYPE = "checkbox" NAME = "answer" VALUE='."$correct".'>'."$answer"."<BR>");
          iscorrect($correct);
          
        }
and here is the part of the function that checks the answers after they are selected

Code: Select all

foreach($_POST["answer"] as $studentanswer)
         {

             
             if($studentanswer == 1):
               $answercount++;  
             endif;
             
             if($answercount == $_SESSION["numcount"]):
               $_SESSION["totalcorrect"]++;
             endif; 
          
         }
I tried googling this but I'm not having a lot of luck finding code that looks any more workable than what I came up with does anyone have any ideas?

Posted: Wed Aug 10, 2005 10:40 am
by shiznatix
gotta make the checkboxes answer an array

Code: Select all

print('<INPUT TYPE = "checkbox" NAME = "answer[]" VALUE="'.$correct.'">'.$answer.'<BR>');

Jcart | fixed quotes

Posted: Wed Aug 10, 2005 10:51 am
by nawhaley
gah! figures! thanks I'll give that a shot.

Posted: Wed Aug 10, 2005 12:34 pm
by nawhaley
ok I must be stupid or something how on earth do you pull a value out of it once you have it in an array format how do you pull anything from it do you just use.

Code: Select all

$_POST["answer"]
and do a foreach like normal to loop through it. do you need to put as value beside it. I'm sorry just finding ANY information on PhP lately has been an absolute horror everyone has a billion guides that do things in a totally obscure way or are so completly sytactically wrong that its just painful to read :(.

Posted: Wed Aug 10, 2005 12:43 pm
by John Cartwright

Code: Select all

<input type="checkbox" name="answer[]" value=1>
<input type="checkbox" name="answer[]" value=2>
<input type="checkbox" name="answer[]" value=3>
<input type="checkbox" name="answer[]" value=4>

Code: Select all

foreach ($_POST['answer'] as $a) {
   echo 'the answer was '.$a;
}
You got it :wink:

Posted: Wed Aug 10, 2005 1:33 pm
by timvw
And a solution for a select box that allows multiple things :)

http://timvw.madoka.be/programming/php/ ... ltiple.txt

Posted: Wed Aug 10, 2005 2:03 pm
by nawhaley
I still seem to be having a problem getting it to check the checkboxes correctly heres the code.

Code: Select all

foreach($_POST["answer"] as $studentanswer)
         {
             
             $_SESSION["answer"] = $answer;
             if($studentanswer == 1):
               $answercount++; 
               
                
             endif;
                $_SESSION["currentanswercount"] = $answercount;
                print('<A HREF="testpost2.php" TARGET = _BLANK> <EM>click here</EM></A>');
          
         }
         
        if($answercount == $_SESSION["numcount"]):
               $_SESSION["totalcorrect"]++;
               print('<A HREF="testpost.php" TARGET = _BLANK> <EM>click here</EM></A>');
             endif;
dont mind the prints their just an attempt at debugging as I cant simply step through the code which is majorly annoying :/. For some reason if I take this part out of the foreach loop.

Code: Select all

if($answercount == $_SESSION["numcount"]):
               $_SESSION["totalcorrect"]++;
               print('<A HREF="testpost.php" TARGET = _BLANK> <EM>click here</EM></A>');
             endif;
it never finds the the answer correct regardless but if I put it inside the foreach loop it counts every checkbox that is checked as correct which is completely wrong.

Posted: Wed Aug 10, 2005 2:37 pm
by nawhaley
the problem is with most examples I"m wanting to do something a slight bit more complicated than just echoing a response though thats a great thing for testing purposes. I need to check the value because it can be either 1 or 0. If its 1 I need it to increase the answer counter that way I can check later on in the script to see if the total number of answers matches the correct number of responses calculated and stord in $_SESSION["numcount"].

Frankly this programs almost a bit much for me given I started learning PHP to specifically do this project and I feel like I'm having to scrape and scrouge to get even the smallest of functions to work due to the very poor documentation on the language itself.

Posted: Wed Aug 10, 2005 8:21 pm
by timvw
Although i don't really understand the problem you are experiencing:
Have you noticed that only the values that where selected are posted back?