Best way to retrieve Checkbox values?

Discussion in 'PHP' started by dawilster, May 14, 2011.

  1. #1
    Hi,

    I'm writing a script and I'm having some problems with retrieving checkbox values, say for example I have for checkbox's values are 1, 2, 3, 4 respectively
    eg.
    • 1
    • 2
    • 3
    • 4

    What I need it do is when the user checks for example 1 and 2 how do I make $check = 1,2 and so fourth.

    $check = 1,2;
    Code (markup):
    Any help would be greatly appreciated, thanks a lot.
     
    dawilster, May 14, 2011 IP
  2. Martin K

    Martin K Active Member

    Messages:
    262
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    put a name attribute to the input tags like:
    and
    $fields = $_POST['fields'];
    will save the selected values in an array;
    You may need some functions dealing with arrays like
    print_r; foreach; the html <pre> tag is also useful.
     
    Martin K, May 15, 2011 IP
  3. mofta7y

    mofta7y Member

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #3
    as Martin Said make the name of the checkbox field as and array "fields[]"


    then in the page that process the form use the following
     
    mofta7y, May 15, 2011 IP