469,269 Members | 1,004 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,269 developers. It's quick & easy.

update check box

333 100+
Hi Guru's
This is a continuation of my last post, but I decided to create a new thread for my new question.

Last post click here:

How to update a check box value to mysql records

Code submitting the check box
# Print the top of a table or headers
echo "<table width='100%'><tr>";
echo "<td><b>Deadline Feedback</b></td>";
echo "<td><b>Check to approved</b></td>";
echo "<td><b>Date Created</b></td>";
echo "<td><b>Author</b></td>";
echo "<td><b>Requestor</b></td>";
echo "</tr>";

#Print data
while($row = mysql_fetch_assoc($result))
echo "<tr border=10><td>". $row['DeadLineFeedback']. "</td>";

#Here where I place the checkbox
echo "<td><input type=\"checkbox\" name=\"approved_$id\"></td>";
echo "<td>". $row['Created']. "</td>";
echo "<td>". $row['Author']. "</td>";
echo "<td>". $row['Requestor']. "</td>";
echo "</tr>";

Code updating the check box

# assigning the checbox rows
$approved = $_POST['approved_$id'];

# This is where I'm having trouble how to tell my php code to pick the checked box

$query = "update fileStorage set approved='Y' where $approved=approved_$id";

# Execute the query
$result = mysql_query($query, $dbLink);

# Check if it was successfull
echo "Success! Your file was updated!";
echo "Error! Failed to update";
echo "<pre>". mysql_error($dbLink) ."</pre>";


Approved value = 'Y' or 'N' only
Sep 12 '08 #1
6 7634
Hi ddtpmyra,

There are two steps to this solution:

1. Properly gathering the data

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <form action='checkboxread.php' method='post'>
  3. <input type='checkbox' name='field[]' value='1'   checked ='True'> Field 1<br>
  4. <input type='checkbox' name='field[]' value='2'> Field 2<br>
  5. <input type='checkbox' name='field[]' value='3'> Field 3<br>
  6. <input type='checkbox' name='field[]' value='4'> Field 4<br>
  7. <input type='checkbox' name='field[]' value='5'> Field 5<br>
  8. <input type='checkbox' name='field[]' value='6' checked ='True'> Field 6<br>
  9. <input type='hidden' name='fieldcount' value='6' >
  10. <input type='submit' value='Submit'>
  11. </form>
  13. </html>
2. Being able to retrieve the checkboxes values

Expand|Select|Wrap|Line Numbers
  1. <?php
  3. echo "<html><head></head><body>";
  5. // retrieve number of checkboxes to process
  6. $fieldcount = (int) $_POST['fieldcount'];
  7. echo "Number of checkboxes to process: $fieldcount </br>";
  9. // set the array to N by default
  10. for ( $count = 1; $count <= $fieldcount; $count++ ) {
  11.    $field[$count] = "N";
  12. }
  14. // retrieve the values that are ON, i.e. TRUE
  15. $checked = $_POST['field'];
  17. // update the array with the TRUE
  18. if (sizeof($checked) > 0) {
  19.    foreach($checked as $item) {
  20.        $field[$item] = "Y";
  21.    }
  22. }
  25. // at this moment you have Y or Ns in your result array
  26. for ( $count = 1; $count <= $fieldcount; $count++ ) {
  27.    echo "field[" .$count . "] = $field[$count] </br>";
  28. }
  30. echo "</body></html>";
  32. ?>


Number of checkboxes to process: 6
field[1] = Y
field[2] = N
field[3] = Y
field[4] = Y
field[5] = N
field[6] = N

So you should be able to retrieve specific values and update your table

Let me know how it goes,

Hope it helps,
Sep 13 '08 #2
333 100+

I can't have assigned field because I can't tell how many check box should I display I depends on the user how many files they will upload. That's why I have check boxes for each rows. And when they check the box I wanted it to be captured and update the records.

So there's only two options equivalent to my check box, Yes and No.

Sep 15 '08 #3
333 100+
anybody can help? I really need to finished this code now :(
Sep 16 '08 #4
5,821 Expert 4TB
How are you generating the inputs? Do you have a fixed number, or can the User add additional inputs using Javascript?
Sep 16 '08 #5
333 100+
i am not knowledgeable on java.
but the script purpose is to display the each data (row) and im placing a check box to update a column name "approved" inside mysql.

this how it works:
the users will upload a file on the database after which the approving person will check the check box if it's approved or not. and here where I get confuse on how will i capture the row id that the approving person click (check) and for my script to execute the update query.

I hope I explain in very well, let me know if you have questions

Sep 17 '08 #6
5,058 Expert 4TB
i am not knowledgeable on java.
Knowing that Java is not JavaScript would be a good start ;)

But, as to you problem.

All you would have to do to print the boxes would be to fetch them from your database and echo an <input> element for each of them.
Somewhat like:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $result = mysqli_query("SELECT `boxID` FROM `boxTable` WHERE `whatever`");
  3. while($row = mysqli_fetch_assoc($result)) 
  4. {
  5.     echo '<br /><input type="checkbox" name="boxList[]" value="'. $row['boxID'] .'" />'. $row['boxID'];
  6. }
  7. ?>
Then, once that is submitted, all the boxes that were checked would be sent as an array named 'boxList' into the $_POST super-global, which you could use to update your database.

You could use the IN clause to update them all at once.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(isset($_POST['boxList'])) 
  3. {
  4.     $inString = implode(", ", $_POST['boxList']);
  5.     $sql = "UPDATE boxTable SET `approved` = TRUE WHERE `boxID` IN($inString)";
  7.     // etc...
  8. }
  9. ?>
This will obviously have to be adapted to fit you data and you other code, but it's the general idea.
Sep 17 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Karaoke Prince | last post: by
2 posts views Thread by joo | last post: by
16 posts views Thread by Philip Boonzaaier | last post: by
5 posts views Thread by Stephen Plotnick | last post: by
6 posts views Thread by Suresh | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.