SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Dealing with multiple Checkboxes

    I've been searching the forum for dealing with multiple checkboxes, but i can't seem to get my head wrapped around what I need to do for my code, since what i've seen doesn't really compare. So here goes... in as much detail as I can get.

    I start with an HTML Form page. This page is just a page to insert a new record into the database. The page has fields for name, location, etc. but it also has checkboxes for what programs the user has access to. Each program has it's own checkbox, if checked I want it to put a 1 in the database field for that program. If unchecked there will be the default 0.
    Here is the HTML code:

    <form method="Post" action="results.php" name="results">
    Last Name: <input type="text" name="LastName" size="30">
    First Name: <input type="text" name="FirstName" size="20">
    Middle Initial: <input type="text" name="MiddleName" size="5">
    <BR>
    Office: <input type="text" name="Office" size="30">
    <BR>
    Network ID: <input type="text" name="NetWork_ID" size="10">
    <BR>
    ACES ID: <input type="text" name="ACES_ID" size="10">
    <BR>
    ACES: <input type="checkbox" name="ACES[]" value="1">
    <BR>
    ACES Online: <input type="checkbox" name="ACES_Online[]" value="1">
    <BR>
    ACES Web Access Only: <input type="checkbox" name="ACES_WEB[]" value="1">
    <BR>
    BarCode: <input type="checkbox" name="BarCode[]" value="1">
    <BR>
    DOH: <input type="checkbox" name="DOH[]" value="1">
    <BR>
    FORS: <input type="checkbox" name="FORS[]" value="1">
    <BR>
    JAS: <input type="checkbox" name="JAS[]" value="1">
    <BR>
    E-JAS: <input type="checkbox" name="E_JAS[]" value="1">
    <BR>
    SEMS: <input type="checkbox" name="SEMS[]" value="1">
    <BR>
    SSPS: <input type="checkbox" name="SSPS[]" value="1">
    <BR>
    <BR>
    Comments: <BR>
    <textarea name="Comments" rows="20" cols="30"></textarea>
    <BR><BR>
    <CENTER><input type="submit" name="Submit" value="Submit"></CENTER>
    </form>

    I tried getting this to work without the "[]" and it wouldn't pass the variable to the next page. I then tired it with the "[]" and only one of the above "programs"... and it worked. When I added in the rest of the "programs"... it didn't work.

    I've read on how to get these checkboxes into an array... but how do i get them back out to get them put into the database into the appropriate field?

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all, please when posting code, enclose it in one of two tags [ code][ /code] or [ php][ /php].

    Secondly, the way you have your form set up you don't need arrays. The way you are using them, you may not have a full understanding of what arrays really are.

    An array is a variable such as $foo that contains multiple values. These multiple values are indexed by either a number, or a key (with keys the array is called an "associative array")

    So, if array $foo were to hold 4 values, we then could assign the values of the array this way:

    $foo = array("a", "b", 3, 4);

    And we could access any one of those values this way:

    echo $foo[0]; would print "a"
    echo $foo[1]; would print "b"
    echo $foo[2]; would print 3
    echo $foo[3]; would print 4

    The numbers between the [] is the index. Indexes in PHP always start at "0".

    An associative array is assigned this way:

    $foo = array("first"=>"a", "second"=>"b", "third"=>3, "fourth"=>4);

    The way we access these values is this way:

    echo $foo['first'] would print "a"
    echo $foo['third'] would print 3
    etc...

    You are, in your form, creating multiple arrays with names like:
    ACES
    ACES_Online
    ACES_WEB
    etc...

    When one array would do, such as:

    name="ACES[]" in each of your checkbox inputs, then you can access each individual value by it's index number.
    Last edited by PHP John; Dec 3, 2002 at 13:16.
    John

  3. #3
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So if i just want to post the value of 1 into the database field i don't need to put them into an array... i can leave off the "[]"???

  4. #4
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are going to assign each of your checkboxes a different name, then yes, you do NOT need an array to do what you are trying to do.
    John

  5. #5
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then can you take a look at this code and tell me what i'm doing wrong? It won't insert it into the database. and in the echo under the While... it won't print that out.

    [ php]
    $query = "insert into allstaff (LastName, FirstName, MiddleName, , Office,
    NetWork_ID, ACES_ID, ACES, ACES_Online, ACES_WEB, BarCode, DOH, FORS, JAS,
    E_JAS, SEMS, SSPS, Comments)
    values ('$LastName', '$FirstName', '$MiddleName', '$Office', '$NetWork_ID',
    '$ACES_ID', '$ACES', '$ACES_Online', '$ACES_WEB', '$BarCode', '$DOH', '$FORS', '$JAS',
    '$E_JAS', '$SEMS', '$SSPS', '$Comments')";
    echo $query;
    $Result = mysql_query($query, $db);
    echo $Result;
    if($Result) echo "submit was OK.";
    $query = "select * from allstaff where LastName = '$LastName'";
    $result = mysql_query($query, $db);
    while($row = mysql_fetch_array($result))
    {
    echo "<B>Last Name/B> $row[LastName]<BR> <B>First Name/B> $row[FirstName]<BR>
    <B>Middle Initial/B> $row[MiddleName]<BR><B>Office/B> $row[Office]<BR>
    <B>Network ID/B> $row[NetWork_ID]<BR> <B>ACES ID/B> $row[ACES_ID]<BR>
    <B>ACES/B> $row[ACES]<BR> <B>ACES Online/B> $row[ACES_Online]<BR>
    <B>ACES On Web/B> $row[ACES_WEB]<BR><B>Barcode/B> $row[BarCode]<BR>
    <B>DOH/B> $row[DOH]<BR> <B>FORS/B> $row[FORS]<BR> <B>JAS/B> $row[JAS]<BR>
    <B>E-JAS/B> $row[E_JAS]<BR><B>SEMS/B> $row[SEMS]<BR> <B>SSPS/B> $row[SSPS]<BR>
    <B>Comments/B> $row[Comments]<BR>";
    [ /php]

  6. #6
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I forgot to tell you to remove the spaces from the php and code tags!
    John

  7. #7
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    huh? you mean just put php /php... or ??

  8. #8
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you making a connection to the database? Is this your complete code?
    John

  9. #9
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i see... here it is again in the proper tags.

    PHP Code:
    $query "insert into allstaff (LastName, FirstName, MiddleName, , Office,
    NetWork_ID, ACES_ID, ACES, ACES_Online, ACES_WEB, BarCode, DOH, FORS, JAS,
    E_JAS, SEMS, SSPS, Comments)
    values ('
    $LastName', '$FirstName', '$MiddleName', '$Office', '$NetWork_ID',
    '
    $ACES_ID', '$ACES', '$ACES_Online', '$ACES_WEB', '$BarCode', '$DOH', '$FORS', '$JAS',
    '
    $E_JAS', '$SEMS', '$SSPS', '$Comments')";
    echo 
    $query;
    $Result mysql_query($query$db);
    echo 
    $Result;
    if(
    $Result) echo "submit was OK.";
    $query "select * from allstaff where LastName = '$LastName'";
    $result mysql_query($query$db);
    while(
    $row mysql_fetch_array($result))
    {
    echo 
    "<B>Last Name:</B> $row[LastName]<BR> <B>First Name:</B> $row[FirstName]<BR>
        <B>Middle Initial:</B> 
    $row[MiddleName]<BR><B>Office:</B> $row[Office]<BR>
        <B>Network ID:</B> 
    $row[NetWork_ID]<BR> <B>ACES ID:</B> $row[ACES_ID]<BR>
        <B>ACES:</B> 
    $row[ACES]<BR> <B>ACES Online:</B> $row[ACES_Online]<BR>
        <B>ACES On Web:</B> 
    $row[ACES_WEB]<BR><B>Barcode:</B> $row[BarCode]<BR>
        <B>DOH:</B> 
    $row[DOH]<BR> <B>FORS:</B> $row[FORS]<BR> <B>JAS:</B> $row[JAS]<BR>
        <B>E-JAS:</B> 
    $row[E_JAS]<BR><B>SEMS:</B> $row[SEMS]<BR> <B>SSPS:</B> $row[SSPS]<BR>
        <B>Comments:</B> 
    $row[Comments]<BR>"

  10. #10
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The second form of the php tag that you did in your reply. See the box? Your code would show up in that box.
    John

  11. #11
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no it's not my complete code... it's just the bulk of it. i'm using an include for my connection. if i comment out all the checkbox values... then the new record is inserted into the database...so i know my connection works... but once i uncomment those values out... it won't work. i've also got a check to see if the connection works and it tells me everytime that it's working.

  12. #12
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post your new form code.
    John

  13. #13
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <body onload="document.results.LastName.focus()">
    <form method="Post" action="results.php" name="results">
    Last Name: <input type="text" name="LastName" size="30">
    First Name: <input type="text" name="FirstName" size="20">
    Middle Initial: <input type="text" name="MiddleName" size="5">
    <BR>
    Office: <input type="text" name="Office" size="30">
    <BR>
    Network ID: <input type="text" name="NetWork_ID" size="10">
    <BR>
    ACES ID: <input type="text" name="ACES_ID" size="10">
    <BR>
    ACES: <input type="checkbox" name="ACES" value="1">
    <BR>
    ACES Online: <input type="checkbox" name="ACES_Online" value="1">
    <BR>
    ACES Web Access Only: <input type="checkbox" name="ACES_WEB" value="1">
    <BR>
    BarCode: <input type="checkbox" name="BarCode" value="1">
    <BR>
    DOH: <input type="checkbox" name="DOH" value="1">
    <BR>
    FORS: <input type="checkbox" name="FORS" value="1">
    <BR>
    JAS: <input type="checkbox" name="JAS" value="1">
    <BR>
    E-JAS: <input type="checkbox" name="E_JAS" value="1">
    <BR>
    SEMS: <input type="checkbox" name="SEMS" value="1">
    <BR>
    SSPS: <input type="checkbox" name="SSPS" value="1">
    <BR>
    <BR>
    Comments: <BR>
    <textarea name="Comments" rows="20" cols="30"></textarea>
    <BR><BR>
    <CENTER><input type="submit" name="Submit" value="Submit"></CENTER>
    </form>
    </body>

  14. #14
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    $query "INSERT INTO allstaff SET
              LastName    = '
    $LastName',
              FirstName   = '
    $FirstName',
              MiddleName  = '
    $MiddleName',
              Office      = '
    $Office',
              NetWork_ID  = '
    $NetWork_ID',
              ACES_ID     = '
    $ACES_ID',
              ACES        = '
    $ACES',
              ACES_Online = '
    $ACES_Online',
              ACES_WEB    = '
    $ACES_WEB',
              BarCode     = '
    $BarCode',
              DOH         = '
    $DOH',
              FORS        = '
    $FORS',
              JAS         = '
    $JAS',
              E_JAS       = '
    $E_JAS',
              SEMS        = '
    $SEMS',
              SSPS        = '
    $SSPS',
              Comments    = '
    $Comments'
              "
    ;

    echo 
    $query;
    $insertResult mysql_query($query$db);

    echo 
    $insertResult;
    if(
    $insertResult) echo "submit was OK.";

    $query "SELECT * FROM allstaff WHERE LastName = '$LastName'";
    $retrieveResult mysql_query($query$db);

    while(
    $row mysql_fetch_array($retrieveResult))
    {
      echo 
    "<B>Last Name:</B> $row['LastName']<BR>
            <B>First Name:</B> 
    $row['FirstName']<BR>
            <B>Middle Initial:</B> 
    $row['MiddleName']<BR>
            <B>Office:</B> 
    $row['Office']<BR>
            <B>Network ID:</B> 
    $row['NetWork_ID']<BR>
            <B>ACES ID:</B> 
    $row['ACES_ID']<BR>
            <B>ACES:</B> 
    $row['ACES']<BR>
            <B>ACES Online:</B> 
    $row['ACES_Online']<BR>
            <B>ACES On Web:</B> 
    $row['ACES_WEB']<BR>
            <B>Barcode:</B> 
    $row['BarCode']<BR>
            <B>DOH:</B> 
    $row['DOH']<BR>
            <B>FORS:</B> 
    $row['FORS']<BR>
            <B>JAS:</B> 
    $row['JAS']<BR>
            <B>E-JAS:</B> 
    $row['E_JAS']<BR>
            <B>SEMS:</B> 
    $row['SEMS']<BR>
            <B>SSPS:</B> 
    $row['SSPS']<BR>
            <B>Comments:</B> 
    $row['Comments']<P>";

    John

  15. #15
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hve you tried to echo your variables before you do anything else in your result script? Just o see if they are caried over from the form?

    What version of Php are you running?

    I'm asking these questions because I think this might be a 'register_globals=off' problem.

    More info here:
    http://www.webmasterbase.com/article/758

    Greetings

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  16. #16
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good call Rik. My brain is addled today...
    John

  17. #17
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm running omni httpd web server with php 4.2.2
    i'm running with globals on because i'm using another script that doesn't work with globals off.


    John... i'll try your script wednesday... my day is over for now. thanks for your help.

    Paul

  18. #18
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok... got it to work... thanks for the help. i did have some trouble at first with your code john because you had single quotes in the echo with $row but once i removed those it worked just great.

    Again Thanks


    Paul


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •