How to use checkbox to select items retrieved from a query?

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

    How to use checkbox to select items retrieved from a query?

    I have a database of skills for our employees. I have created a form, based on a query, that displays a users skills which have the "achieved" field set to Null (which indicates that they have not achieved the desired skill). The form displays the SkillID number, the skill description, and a checkbox for the user to click. When they click the Update button, it would set the field "achieved" to "Yes" for each SkillID that the user selects.

    My problem is, I do not know how to set the value of the checkbox to be whatever the SkillID is. I can make a checkbox form using a set value, but because the values are being retrieved from a query, they cannot be hard coded into the query/form. Can anyone help with this? I am totally lost. The skills needing to be updates all display accurately, now I just have to make it so users can do something with them!

    Here is some code that may help. This is the code for the form. I know I need more, to process the form input, but here is where I get stuck, as I don't know how to process the input.
    Code:
    <?php
    session_start();
    $fname = $_SESSION['fname'];
    $lname = $_SESSION['lname'];
    $username = $_SESSION['username'];
    $user_skillID = $_POST['user_skillID'];
    $ud_userskillID = serialize($_POST['ud_userskillID']);
    $tngdate = date("d-M-y");
    require 'Includes/Header.php';
    require_once('Connections/skillsdb.php');
    ?>
    <html>
    <head>
    <title>Microsoft Access 2007 Skills Listing</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="Includes/si.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body topmargin="5" leftmargin="0">
    <table width="980" align="center" cellpadding = "0">
    <td>
    <tr>
    <th align="center">
       <b>Microsoft Access 2007 skills for 
    <?php
    echo $_SESSION['fname'];
    echo '&nbsp;';
    echo $_SESSION['lname'];
    echo ":";
    ?> 
    <br/><p>(If no skills appear, there are no skills for you to update.)</p>
    </th>
    </tr>
    </td>
    <tr>
    <td>
    <?php require 'Includes/topicsmenu.php'; ?><br />
    </tr>
    </td>
    </table ><br />
    
    <table width="800" align="center" border="1">
    <td>
    <tr>
    		<th>Skill ID</th>
    		<th>Skill Description</th>
    		<th>Select Achieved Skill(s)</th>
    		
    </tr>
    </td>
    
    <?php
    mysql_select_db($database_skills, $skills);
    
    $query_MySkills = "SELECT user_skills.user_skillID, skills.topic, skills.skill, user_skills.tngdate, user_skills.achieved, users.username, DATE_FORMAT(`tngdate`,'%d-%b-%y') AS tngdate
                   FROM skills, users, user_skills
                   WHERE users.username = ('$username') AND users.userID=user_skills.userID AND user_skills.achieved != 'N/A' AND user_skills.achieved !='Yes'
    		AND user_skills.skillID=skills.skillID
    		AND skills.topic = 'Access'
                   ORDER BY topic, skill";
    
    $MySkills = mysql_query($query_MySkills, $skills) or die(mysql_error());
    //$row_MySkills = mysql_fetch_assoc($MySkills);
    $totalRows_MySkills = mysql_num_rows($MySkills);
    
    $result = mysql_query($query_MySkills, $skills);
    
    while($row_MySkills = mysql_fetch_assoc($MySkills))
    		{
    		echo '<tr>';
    		echo '<td align="center">' . ($row_MySkills['user_skillID']).'</td>';
    		echo '<td align="left">&nbsp;&nbsp;' . ($row_MySkills['skill']). '</td>';
    		echo '<td <form method="POST" action="changeskillsrecord.php">
    <p align="center"><input type="checkbox" name="user_skillID[]" value="$ud_userskillID">';	
    		
    		echo '</tr>';
    		}
    ?>
    <br /><br />
    <td>
    <td>
    <td>
    <p align="center"><input type="submit" value="Update"></p>
    </form>
    </td>
    </td>
    </td>
    </table>
    </body>
    </html>
    Last edited by Joni Seth; Nov 3 '10, 02:24 PM. Reason: Forgot code tags.
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    shouldn't user_skillID[] be filled in with the id of that skill? so you know what to update later? You're storing a serialized $_POST var in it. You have not given me any clue as to what's in "ud_userskillID " and where it comes from.

    checkmarks are not any different than any other input forms like text or select when it comes to processing it. They all have a name and values. The name should be something you could identify with PHP to do something with the value.



    I'd like to point out a few other things:

    1. You should do your processing (queries) before you even output the HTML. Take the results into a variable and just echo it. There's no point in having PHP echo all that HTML string that it doesn't do much with.

    2. Your HTML does not validate. You're using <tr> inside a <td>. It should be the opposite. <td> should be inside a <tr> Run your html output through the w3c.org's validator. It will prevent some headaches later and will probably solve problems you didn't think you had (or make them apparent!).

    3.

    Comment

    • Joni Seth
      New Member
      • Jul 2010
      • 6

      #3
      Ah, you're right about the "serialize" . I've been trying so many different things, I've got junk in there that doesn't need to be. I removed it.

      Your question "shouldn't user_skillID[] be filled in with the id of that skill? so you know what to update later?" - YES! How do I do that? The query outputs to the screen so that the user can see what skills are available (i.e. not achieved) for them to learn and check off when they learn them. I was able to create a form where the user actually typed in the skillID number one at a time and it did work, but my boss said "that's not user friendly", and wants checkboxes.

      They all have a name and values. The name should be something you could identify with PHP to do something with the value.

      The value needs to be the user_skillID of the skill that the user checks, but then it needs to update the field "achieved" to "Yes" when submitted. When I click a checkbox and then do "echo $user_skillID", the output is "Array", not the actual record(s) selected.

      Comment

      • Joni Seth
        New Member
        • Jul 2010
        • 6

        #4
        Here's a better description of what I'm trying to do:
        "Many times we have to display a list of names or records for consideration and out of which user can select any number of records for further action. Here we may not know exactly how many records will be displayed as some time based on different conditions records will be collected from a table. So we can list the records with a checkbox by the side of it and user can select any number of records. We will have one unique id associated with each record and that will be used as value of the check box. The "one unique id associated with each record" already exists, so I don't need to have php count them for me to give them a unique number. I just don't know how to get that existing unique identifier number (user_skillID) to be associated with its checkbox. Does that make any more sense? Thank you.

        Comment

        Working...