SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    inserting multiple checkbox values into a field

    Hi, I have a form of which one element is a series of checkboxes. I want to be able to tick multiple checkboxes and have the values stored in the db. I'm guessing I need to turn them into an array somehow, but I don't really know how to do it and if I'd need to change my function code?

    Here's what I have:
    PHP Code:
    <?php
    // Add a record
        
    function add_record($params)
        {
          
    $connection db_connect();        
          
    $query sprintf("insert into records set 
                                             Name = '%s',
                                             Number = '%s',
                                             Colours = '%s'"

                                
    mysql_real_escape_string($params['Name']),
                                
    mysql_real_escape_string($params['Number']),
                                
    mysql_real_escape_string($params['Colours'])
                                );        
          
    $result mysql_query($query);
            if (!
    $result)
            {
              return 
    false;
            }
            else
            {
              return 
    true;
            }        
        
    ?>
    <input type="checkbox" name="Colours" value="Red"  />Red
    <input type="checkbox" name="Colours" value="Blue"  />Blue
    <input type="checkbox" name="Colours" value="Green"  />Green
    <input type="checkbox" name="Colours" value="Yellow" />Yellow
    <input type="checkbox" name="Colours" value="Black"  />Black
    <input type="checkbox" name="Colours" value="White" />White

  2. #2
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can have the checked values put into a $_POST array like so:

    HTML Code:
    <input type="checkbox" name="Colours[]" value="Red"  />Red
    <input type="checkbox" name="Colours[]" value="Blue"  />Blue
    <input type="checkbox" name="Colours[]" value="Green"  />Green
    <input type="checkbox" name="Colours]" value="Yellow" />Yellow
    <input type="checkbox" name="Colours[]" value="Black"  />Black
    <input type="checkbox" name="Colours[]" value="White" />White
    aaron-fisher.com - PHP articles and more


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
  •