Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Checkboxes

  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Checkboxes

    Hi, I generate a form page from my MySQL database.
    let's say the table structure is:

    Table A
    > ID, title, active

    the number of rows generated by the query is dynamic. If a field's value is 1, then the form output appears as

    PHP Code:
    <input type=checkbox name=$ID checked$title 
    and if not

    PHP Code:
    <input type=checkbox name=$ID$title 

    At the bottom of the form there is a submit button. The visitor can check/uncheck rows and then submit the form.

    I need to write a loop (possibly foreach) that takes the values and processes them and writes the changes to the database.

    What should I do?
    Last edited by guvenck; 02-12-2006 at 10:24 AM.

  • #2
    New Coder
    Join Date
    Jan 2006
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Set the name of the checkbox to check_id[], and the value to $id

    Then work with this code to make it work :P

    PHP Code:
        if ($_POST['check_id']) {
            foreach (
    $check_id as $checkid) {
            
    //SQL queries here. $checkid is the id of the checkboxes that are selected.
            
    }


  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Zegg, thanks for your answer. However, if a checkbox is not checked, it is not being submitted. So if you uncheck a previously checked checkbox and submit it, it is not submitted. How can I check if it has changed and update the change in the database?

  • #4
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Any form attribute in between the form tags will be sent upon submit unless you simply don't show them, similar to Zegg's code. Or you can simply not give the checkboxes id's or names so they won't be used.

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Element, if a checkbox is not checked, it is not submitted. Can you exactly get my point? Or would you explain your post a little?

  • #6
    New Coder
    Join Date
    Jan 2006
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The code I posted loops through each checkbox that's checked. It doesn't matter how many times a person checks or unchecks a checkbox. It just depends on what he submits.


  •  

    Posting Permissions

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