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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

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

Post 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?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

Post by nawhaley »

gah! figures! thanks I'll give that a shot.
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

Post 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 :(.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

And a solution for a select box that allows multiple things :)

http://timvw.madoka.be/programming/php/ ... ltiple.txt
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

Post 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.
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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?
Post Reply