Creating a checkbox array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Joni Seth
    New Member
    • Jul 2010
    • 6

    #1

    Creating a checkbox array

    I have a database of required skills for employees. There is a many to many relationship between the users table and the skills table, called user_skills. It contains the following fields: auto increment ID called user_skillsID, the foreign keys userID from the users table, and skillID from the skills table, a date field called tngdate and a "set" field called achieved. I have a form (called "updateskills.p hp") that the employees can open which brings up their un-achieved skills and presents a checkbox for them to check the skills that they can perform. The updateskills.ph p form action goes to "processskills. php", and when a checkbox is checked and the submit button is clicked, "processskills. php" opens, and an echo of the query displays: UPDATE user_skills SET achieved = 'Yes'WHERE user_skillID=62 84. The problem is that I have clicked the skill with the user_skillID= 6191, not 6284. 6284 is the last skill in the list. For my checkbox, I have the code as:

    <input type="checkbox" name="achieved[]" value="Yes">

    which is supposed to create an array, but it appears that it is NOT creating an array. I do not know how to fix this. No matter what I do, it keeps updating only the last record, no matter how many checkboxes I select. Each checkbox, however, does appear when I echo the $_REQUEST. Does anyone have any suggestions? I will be glad to post additional code if need be, but all else seems to be working correctly. Thank you in advance.
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    You must have misspelled something somewhere.

    The following code works just as you described:

    Code:
    <?php
    
    
    if(isset($_REQUEST['foo']))
    {
        die(print_r($_REQUEST['foo']));
    }
    
    ?>
    <html>
    <body>
    <form action="" method="post">
    
    <input name="foo[]" type="checkbox" value="1" /> 1 <br />
    <input name="foo[]" type="checkbox" value="2" /> 2<br />
    <input name="foo[]" type="checkbox" value="3" /> 3<br />
    <input name="foo[]" type="checkbox" value="4" /> 4<br />
    
    <input type="submit" />
    </form>
    </body>
    </html>
    Check to see if all your check box's values are not the same. (Right Click -> View Source in your browser)


    Dan

    Comment

    • Joni Seth
      New Member
      • Jul 2010
      • 6

      #3
      Hi. Thanks for your reply. Here is some of my page source:

      <tr><input type="hidden" name="check_sub mit" value="1"/>
      <input type="hidden" name="user_skil lID" value="6191"/>
      <td align="center"> &nbsp;&nbsp;619 1</td>
      <td align="left">&n bsp;&nbsp;Acces s</td>
      <td align="left">&n bsp;&nbsp;Can you export data from a database?</td>
      <td align="center"> <input type="checkbox" name="achieved[]" value="Yes"></td></tr>
      <tr><input type="hidden" name="check_sub mit" value="1"/>
      <input type="hidden" name="user_skil lID" value="6192"/>
      <td align="center"> &nbsp;&nbsp;619 2</td><td align="left">&n bsp;&nbsp;Acces s</td>
      <td align="left">&n bsp;&nbsp;Can you run an existing query on a database?</td>
      <td align="center"> <input type="checkbox" name="achieved[]" value="Yes"></td></tr>
      <tr><input type="hidden" name="check_sub mit" value="1"/>
      <input type="hidden" name="user_skil lID" value="6193"/>
      <td align="center"> &nbsp;&nbsp;619 3</td><td align="left">&n bsp;&nbsp;Acces s</td>
      <td align="left">&n bsp;&nbsp;Can you run an existing report on a database?</td>
      <td align="center"> <input type="checkbox" name="achieved[]" value="Yes"></td></tr>
      <tr><input type="hidden" name="check_sub mit" value="1"/>

      It appears that the checkboxes actually do have the same name. Somehow they need to get associated with the user_skillID, which is what they are actually updating as achieved.

      Playing around with some echo statements, here is what is displayed after I click three checkboxes:

      Yes
      Yes
      Yes

      Your skills: 6284

      UPDATE user_skills SET achieved = 'Yes'WHERE user_skillID=62 84

      <pre&gtArray ( [check_submit] => 1 [user_skillID] => 6284 [achieved] => Array ( [0] => Yes [1] => Yes [2] => Yes ) [Submit] => Submit ) 1</pre>

      <pre&gtArray ( [fname] => Patricia [lname] => Canning [username] => pcanning ) 1</pre>
      1achieved skill(s) are updated in the database.

      The first 3 "Yeses" are displaying each checkbox I checked.

      The "Your skills: is echo "Your skills {$_POST['user_skillid']}.

      The UPDATE user_skills is my query echo - here is where the rub appears: only one record, the LAST record, is selected to be updated, even though I selected three checkboxes, which shows me that some how the array is wrong, since my "pre&gtArra y" display does show an array of something, but apparently it isn't right since it isn't doing what I need it to do. That's my problem, I don't know how to tell it to do what I need it to do!

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        You need to put the skill_id where you put "Yes". You see how in my example that I gave you, I had different values? 1,2,3,4? if those were all the same, I'd have no idea what was selected!

        skill_id must be the value of the checkbox.

        Right now you have 3 check boxes all of them have the same name and the same value. So that's what you're going to get.

        If you have checkboxes of the same name[] but different values, you will get all the values that were checked.

        Here's some rough pseudocode of what you need to do:

        Code:
        <?php
        
        if($_POST['submit'])
        {
          $skills = $_POST['achieved']; 
        
          // connect to mysql
        
          foreach ($skills as $skill)
          {
            $skillID = mysql_real_escape_string($skill); 
            $sql = "UPDATE user_skills SET achieved = 'Yes' WHERE user_skillID = "$skillID"; 
        
          }
        }
        ?>
        <html>
        <body>
        <form action="" method="post" name="form1">
        Some Skill: <input type="checkbox" name="acheived[]" value="6484" />Yes
        <br/>
        Another Skill: <input type="checkbox" name="acheived[]" value="1234" />Yes
        <br/>
        ...
        <input type="submit" name="submit"/>
        </form>
        </body>
        </html>
        Got it?




        Dan

        Comment

        • Joni Seth
          New Member
          • Jul 2010
          • 6

          #5
          Originally posted by dlite922
          You need to put the skill_id where you put "Yes". You see how in my example that I gave you, I had different values? 1,2,3,4? if those were all the same, I'd have no idea what was selected!

          skill_id must be the value of the checkbox.

          Right now you have 3 check boxes all of them have the same name and the same value. So that's what you're going to get.

          If you have checkboxes of the same name[] but different values, you will get all the values that were checked.

          Here's some rough pseudocode of what you need to do:

          Code:
          <?php
          
          if($_POST['submit'])
          {
            $skills = $_POST['achieved']; 
          
            // connect to mysql
          
            foreach ($skills as $skill)
            {
              $skillID = mysql_real_escape_string($skill); 
              $sql = "UPDATE user_skills SET achieved = 'Yes' WHERE user_skillID = "$skillID"; 
          
            }
          }
          ?>
          <html>
          <body>
          <form action="" method="post" name="form1">
          Some Skill: <input type="checkbox" name="acheived[]" value="6484" />Yes
          <br/>
          Another Skill: <input type="checkbox" name="acheived[]" value="1234" />Yes
          <br/>
          ...
          <input type="submit" name="submit"/>
          </form>
          </body>
          </html>
          Got it?




          Dan
          Hi Dan,
          Thank you for taking the time to post your reply. What you posted is exactly what I'm having trouble with. The first problem seems to be that the field "user_skill.use r_skillID" really doesn't equal anything else in the database. It is an auto increment field in a join table for a many-to-many relationship. $skillsID can't equal mysql_real_esca pe_string($skil l) because it's not text, it's a number. The skills that appear for a user to check that they are achieved are acquired from a query, so how can I enter the value when I don't know what that value will be?
          Anyway, no matter how I enter the code, I get a blank white page when I run it, so it seems I have something messed up somewhere. Good try though.

          Comment

          Working...