0

I need to use a form in the front-end that allows people to post. I have tried to keep the minimum code in order to focus only on the issue (mentioned below) at hand.

EXAMPLE FORM:

    <form method="POST" id="test_form" name="test_form" enctype="multipart/form-data" action="">

          <div>LOCATION : <input type="text" name="location" id="location"/></div>           

    <div>CHECKBOXES : 
         <input type="checkbox" name="fruits[]" value="orange"/>Orange</br>
         <input type="checkbox" name="fruits[]" value="apple"/>apple</br>
         <input type="checkbox" name="fruits[]" value="banana"/>banana
     </div>

 <input type="hidden" name="action" value="post_action" />
 <input type="submit" name="submit" id="submit" value="PROCEED"/>

EXAMPLE PROCESSING :

if( 'POST' == $_SERVER['REQUEST_METHOD'] &&  !empty( $_POST['action'] ) && $_POST['action'] == "post_action") {

    if (isset($_POST['submit'])) {
        $error = "";

        if ($_POST['location'] != null) {
        $location = trim(strip_tags($_POST['location']));
        } else {
        $error .= 'Put location.</br>';
        }           
    } 

            $fruits= $_POST['fruits'];

        if (empty($error)) {   
        $new_post = array(   //insert form inputs, set var and define array
        'post_title'    =>  $location, 
        'post_content'  =>  $description,
        'post_status'   =>  'draft',
        'post_author'   =>  '2',
        'fruits'        => $fruits,
        'post_type'     => 'post'  
        // assigning tags and categories are no issue
        );

        $pid = wp_insert_post($new_post);

    //ADD OUR CUSTOM FIELDS 
    add_post_meta($pid, 'fruits', $fruits, false);                          

    } 
}

THINGS TRIED : I have searched a lot on net, results were either overly complex or too project-specific. Also most of the results in this forum search came out related either to custom-post-types, custom fields or meta-boxes only. I now have a partial solution though. I have followed this tutorial and was able to get a single custom-field key-value pair in my post by using add_post_meta($pid, 'fruits', $fruits, false);.

ISSUE : So basically what I am looking is to get all the values of the checkboxes, whichever is ticked. I shall greatly appreciate any solution suggested in an uncomplicated manner. Thanks.

1

For multiple selections to work the name attribute of your form has to be an array, you can specify that like this: name="fruits[]" - or as complete line:

    <input type="checkbox" name="fruits" id="fruits[]" value="apple" />

This is well documented, for example:

7
  • thanks, I have gone through the resources pointed out but still am cluless on how do I insert selected multiple values in wordpress
    – gurung
    Oct 9 '13 at 12:30
  • The fruits variable is an array of the selected values now and will be saved into post meta as serialized string - take a look at: add_post_meta()
    – Nicolai
    Oct 9 '13 at 12:37
  • Please note that I have updated my form inputs. Secondly my rest of the code should work now then. right? Because I am getting all the values into the created posts except the checkbox values.
    – gurung
    Oct 9 '13 at 12:43
  • I just saw that the value attribute on your type="hidden" input doesn't match the name attribute of your form. You might want to change that.
    – Nicolai
    Oct 9 '13 at 12:52
  • pl ignore that part as the snippet put here is extremely trimmed part of a fairly complex multi part form with sessions previews and what not. Trust me, Everything else works as expected.
    – gurung
    Oct 9 '13 at 13:01

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.