0

I've many check-boxes. I'd like to pull their values into a comma separated array. If checkbox is diselected value will be empty so:

bar,parking,,,,,tv,etc

how would I do this? after making an array I will submit into a db.

    <p>
        <label for="meta_box_check_bar">bar</label>
        <input type="checkbox" id="meta_box_check_bar" name="meta_box_check_bar" value="bar" />

        <label for="meta_box_check_parking">parking</label>
        <input type="checkbox" id="meta_box_check_parking" name="meta_box_check_parking" value="parking" />

        <label for="">accessible-for-disabled</label>
        <input type="checkbox" id="meta_box_check_accessible-for-disabled" name="meta_box_check_accessible-for-disabled" value="accessible-for-disabled" />

        <label for="">air-conditioning</label>
        <input type="checkbox" id="meta_box_check_air-conditioning" name="meta_box_check_air-conditioning" value="air-conditioning" />

        <label for="">frigobar </label>
        <input type="checkbox" id="meta_box_check_frigobar" name="meta_box_check_frigobar" value="frigobar" />

        <label for="">pets</label>
        <input type="checkbox" id="meta_box_check_pets" name="meta_box_check_pets" value="pets" />

        <label for="">phone</label>
        <input type="checkbox" id="meta_box_check_phone" name="meta_box_check_phone" value="phone" />

        <label for="">tv</label>
        <input type="checkbox" id="meta_box_check_tv" name="meta_box_check_tv" value="tv" />

        <label for="">typical-local-dishes</label>
        <input type="checkbox" id="meta_box_check_typical-local-dishes" name="meta_box_check_typical-local-dishes" value="typical-local-dishes" />
    </p>
4
  • Never ever put more than ne information in one database field. Commented Jun 26, 2012 at 22:59
  • why not? In this case I don't want to fill db with 1-2 word entries per row. Commented Jun 26, 2012 at 23:08
  • If you want to select rows with a bar, you need a full table scan with a regex or some string fancy - this will kill your performance. Really. Commented Jun 26, 2012 at 23:10
  • I only need to grab whole array, and than explode it with php. Will this kill performance? So I select ID of the row, I don't need to select bar alone. Commented Jun 27, 2012 at 6:55

5 Answers 5

1
/* make an array for all used checkbox */
$used_checkboxes = array();

/* make an array whit all options */
$avaible_checkboxes = explode(',', "bar,parking,accessible-for-disabled,air-conditioning,frigobar,pets,phone,tv,typical-local-dishes");


/* loop troguht all avaible checkboxes */
foreach($avaible_checkboxes as $current_key)
{
   /* check if the checkbox was sent */
   if(isset($_POST["meta_box_check_{$current_key}"]))
   {
      /* if sent, add key to list */
      $used_checkboxes[$current_key] = $current_key;
   }
   else
   {
      /* if not sent, add empty value to list */
      $used_checkboxes[$current_key] = '';
   }
}

/* convert list to csv */
$used_checkboxes_csv = implode(',', $used_checkboxes);
Sign up to request clarification or add additional context in comments.

Comments

0

Name your fields checkboxes[], then in PHP you can get a similar array with $_GET['checkboxes'].

1 Comment

this will not give him empty values
0

Sounds like a job for ajax to me. I would consider dojo or jquery to collect and pass the data.

1 Comment

Well, those controls are client side controls and are not immediately available to the server. Either way you will have to have some mechanism to get the data from the client's browser to the hosting server. Ajax is as good a method as any.
0

You should give them all the same name for the name field that way when you do the get on the name it'll return array of all those values that are checked.

Here's a reference you can read about: http://www.html-form-guide.com/php-form/php-form-checkbox.html

2 Comments

this will not give him empty values
Ah didn't read the fact that there should be empty values, good catch.
0

If possible, i recomend you to use [] in the variable names to pass an array and retrieve their values in the PHP file. Something like this:

HTML

<p>
    <label for="meta_box_check_bar">bar</label>
    <input type="checkbox" id="meta_box_check_bar" name="array[]" value="bar" />

    <label for="meta_box_check_parking">parking</label>
    <input type="checkbox" id="meta_box_check_parking" name="array[]" value="parking" />

    <label for="">accessible-for-disabled</label>
    <input type="checkbox" id="meta_box_check_accessible-for-disabled" name="array[]" value="accessible-for-disabled" />

    <label for="">air-conditioning</label>
    <input type="checkbox" id="meta_box_check_air-conditioning" name="array[]" value="air-conditioning" />

    <label for="">frigobar </label>
    <input type="checkbox" id="meta_box_check_frigobar" name="array[]" value="frigobar" />

    <label for="">pets</label>
    <input type="checkbox" id="meta_box_check_pets" name="array[]" value="pets" />

    <label for="">phone</label>
    <input type="checkbox" id="meta_box_check_phone" name="array[]" value="phone" />

    <label for="">tv</label>
    <input type="checkbox" id="meta_box_check_tv" name="array[]" value="tv" />

    <label for="">typical-local-dishes</label>
    <input type="checkbox" id="meta_box_check_typical-local-dishes" name="array[]" value="typical-local-dishes" />
</p>

PHP

// Prevent errors when nothing is checked
if( ! is_array($_POST['array']) )
    $_POST['array'] = array();

// Possible items
$possible_item[] = "bar";
$possible_item[] = "parking";
$possible_item[] = "accessible-for-disabled";
$possible_item[] = "air-conditioning";
$possible_item[] = "frigobar";
$possible_item[] = "pets";
$possible_item[] = "phone";
$possible_item[] = "tv";
$possible_item[] = "typical-local-dishes";

// Starting output string
$output = '';

// Loop the possible items
foreach($possible_item as $value)
{
    // If item is in POST array, add to the output string, else put just comma if needed
    if( in_array($value, $_POST['array']) )
        $output .=  ( empty($output) ? '' : ',' ) . $value;
    else
        $output .=  ( empty($output) ? '' : ',' );
}

echo $output;

2 Comments

You should read the other answers, Puggan has pointed out that most answers have been missing the fact that the requester wants empty values.
@richardhsu, Thanks for advise me. But even in this case, he can compare in the PHP file what values was passed, if not passed, he can consider that the filed is empty. It is my opinion, i will try to edit my answer with this option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.