Checking if checkboxes, created from while loop, are checked

Discussion in 'PHP' started by DonTino, Mar 13, 2008.

  1. #1
    Hi Sitepointers,

    I have a small problem finding a solution for my following problem:
    I need to check many checkboxes (created form a loop) to see if they have been checked.

    I create the checkbox form fields out of a sql table using a while loop like this:

    
    while ($r = mysql_fetch_array($result)) {   
        extract ($r);
       
       echo "<input type=\"checkbox\" name=\"offer$id\" value=\"$headline, \"$check>
    ";       
        }
    
    }
    
    Code (markup):
    The HTML of the output would look something like this:
    
    
    <input type="checkbox" name="offer1" value="Adventure Offer A, ">
    <input type="checkbox" name="offer2" value="Adventure Offer B, ">
    <input type="checkbox" name="offer3" value="Adventure Offer C, ">
    <input type="checkbox" name="offer4" value="Adventure Offer D, ">
    .
    .
    .
    and so on
     
    
    
    Code (markup):
    This is great an lets me dynamically create as many checkboxes as needed depending on the offers in the database.

    However how can I check which ones of them have been checked?

    If there'd be just one with a fixed name I'd do something like this in the while loop:

    
    if ($offer1 == "$headline, ") {$check = "checked";}
    else {$check = "";}
    
    Code (markup):
    However now I get more than 1 offer, I get $offer1 $offer2... and so on.

    How can check on each one???

    I tried and all my ideas failed, any help out here?

    Thanks a lot.

    Tino
     
    DonTino, Mar 13, 2008 IP
  2. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    :D

    Copy & Paste job, eh... ;)

    Anyway, your question has already been answered there.
     
    CreativeClans, Mar 13, 2008 IP
  3. DonTino

    DonTino Peon

    Messages:
    131
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    :D LOL you got me yes,

    sorry about that totally forgot to change :|

    Yes has been answered there but I'm still stuck, I don't seem to be able to code it. I'd need a small sample. Never coded that way before :(

    Anyone wants to help providing a short sample for me??

    here's the answer I got from sitepoint:

    Thanks
     
    DonTino, Mar 13, 2008 IP
  4. DonTino

    DonTino Peon

    Messages:
    131
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ok I understood don't worry about it

    Tino
     
    DonTino, Mar 13, 2008 IP