I am working on a custom theme where there is option to upload images . I have certain check-boxes in the form which the users select as per where the image has to go. My form code is as follows :

<input type="checkbox" class="input" name="vtype[]" value="3"/>
<input type="checkbox" class="input" name="vtype[]" value="4" />
<input type="checkbox" class="input" name="vtype[]" value="5" />
<input type="checkbox" class="input" name="vtype[]" value="6" />

function for using the values is :

$image_category = implode(',', $_POST['vtype']);

if i echo this then the result is fine e.g. 3,4,5 . but if i try to use this value to insert values in database then the value is updated in only the 1st category and nothing happens for the remaining.

main function is inserting values in database is :

$post = array(
      'ID' => '',
      'post_author' => $image_author, 
      'post_category' => array($image_category),
      'post_content' => $image_to_attach, 
      'post_title' => $image_title,
      'post_status' => 'publish'
    // Insert the values in DB
    $id = wp_insert_post($post);
  • You are parsing a string as an array($image_category)? Isn't $image_category already an array? – kaiser May 12 '11 at 15:43

Currently you are passing on this value:

'post_category' => array('3,4,5') // This is a single string

While you should be doing this:

'post_category' => array(3,4,5) // Three separate values

Don't forget to sanitize the POST values neither:

// Initialize categories
$post_category = array();

// Prevent "undefined variable" error notices
if (isset($_POST['vtype']))
  // Loop over selected categories
  foreach ((array) $_POST['vtype'] as $vtype)
    // Validate vtype (only numbers allowed)
    if (ctype_digit((string) $vtype))
      // Add category
      $post_category[] = (int) $vtype;

// Save the post with $post_category in database as you did before...

You can shorten this whole block to a oneliner as well, if you like. Just for fun:

$post_category = (isset($_POST['vtype'])) ? array_filter((array) $_POST['vtype'], 'ctype_digit') : array();
  • Thanks Geert that works great , one more thing i wud like to ask. Can i add more category ID to the code which is not the part of $_POST['vtype']. – Harjeet Singh May 12 '11 at 17:54
  • @Harjeet: of course. It is a matter of adding values to the array, which can be done in many ways. Have a read in the PHP manual. $post_category[] = 'new value'; – Geert May 12 '11 at 18:32
  • @Geert: i am completely lost at the moment , can you please let me know how can i add value for $_POST['ctype'] in the above code along with $_POST['vtype']. Thanks in advance. – Harjeet Singh May 12 '11 at 18:44
  • @Geert: i got the point i added $post_category[] = (int) $ctype; just below $post_category[] = (int) $vtype; and it worked. I hope this is the correct way to do it .... – Harjeet Singh May 12 '11 at 18:59
  • @Harjeet: I'm not completely following anymore, but it seems you just need to polish your PHP array skills. ;) Check out this tutorial. – Geert May 12 '11 at 19:23

another way for submitting multiple values using checkbox, enjoy :)

'day'=> $days ); $format=array( '%s'); $succes=$wpdb->insert( $table, $data, $format ); if($succes){ echo 'Your values has been submitted'; } else{ echo "error in submitting"; } } ?> Day: Any Day: Sun: Mon: Tue: Wed: Thu: Fri: Sat:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.