logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$5
Custom Meta Box Checkbox Issue

I have registered the following Checkbox in a Custom Meta Box.
It works however I want to tick multiple boxes, when I click three it will only save one, usually the first checked.

So I am after a solution to this issue.

echo'<label for="accreditations">' . __("Accreditations", 'myplugin_textdomain' ) . '</label>';

echo '<input type="checkbox" name="accreditations" value="aaa_tourism"';
if($accreditations == 'aaa_tourism'){echo 'checked';}else{echo '';};
echo' /> AAA Tourism';

echo '<input type="checkbox" name="accreditations" value="accredited_tourism_business_australia"';
if($accreditations == 'accredited_tourism_business_australia'){echo 'checked';}else{echo '';};
echo '/> Accredited Tourism Business Australia ';

echo '<input type="checkbox" name="accreditations" value="environmentally_friendly"';
if($accreditations == 'environmentally_friendly'){echo 'checked';}else{echo '';};
echo '/> Environmentally Friendly';

This question has been answered.

parksey18 | 03/30/11 at 10:34pm Edit


(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    03/30/11
    11:09pm
    Lew Ayotte says:

    You need to make sure $accreditations is an array value that contains any of the checked values... then you need to set the input name to accreditations[] (to signify the HTML can contain multiple values). When you check, simply do the in_array function. By the way, WordPress has a nifty helper function called "checked" it will output the proper HTML if the value is true. You're output "checked" isn't valid HTML in all browsers...

    See this code:

    <?php

    echo '<label for="accreditations">' . __("Accreditations", 'myplugin_textdomain' ) . '</label>';

    ?>
    <input type="checkbox" name="accreditations[]" value="aaa_tourism" <?php checked( in_array( 'aaa_tourism', $accreditations ) ); ?> /> AAA Tourism

    <input type="checkbox" name="accreditations[]" value="accredited_tourism_business_australia" <?php checked( in_array( 'accredited_tourism_business_australia', $accreditations ) ); ?> /> Accredited Tourism Business Australia

    <input type="checkbox" name="accreditations[]" value="environmentally_friendly" <?php checked( in_array( 'aaa_tourism', $environmentally_friendly ) ); ?> /> Environmentally Friendly

    • 03/31/11 12:10am

      parksey18 says:

      So what would the code be for a custom meta box?
      As when I replace the code it is full of errors?

    • 03/31/11 8:16am

      Lew Ayotte says:

      I disagree with AdamGold's PHP code... I would do this:



      if ( isset( $_POST['accreditations'] ) ) {
      update_option( 'accreditation', $_POST['accreditations'] );
      }


      WordPress will automatically serialize the array into one option.

      You're probably getting errors because your variable isn't initialized properly. I'd have to see your actual code block to tell you what you've done wrong.

  • avatar
    Last edited:
    04/01/11
    6:47am
    AdamGold says:

    The HTML code:

    <?php



    echo '<label for="accreditations">' . __("Accreditations", 'myplugin_textdomain' ) . '</label>';



    ?>

    <input type="checkbox" name="accreditations[]" value="aaa_tourism" <?php checked( in_array( 'aaa_tourism', $accreditations ) ); ?> /> AAA Tourism



    <input type="checkbox" name="accreditations[]" value="accredited_tourism_business_australia" <?php checked( in_array( 'accredited_tourism_business_australia', $accreditations ) ); ?> /> Accredited Tourism Business Australia



    <input type="checkbox" name="accreditations[]" value="environmentally_friendly" <?php checked( in_array( 'aaa_tourism', $environmentally_friendly ) ); ?> /> Environmentally Friendly


    The PHP code:

    $accreditations = $_POST['accreditations'];
    $i = 0;
    foreach( $accreditations as $value ) {
    update_option('accreditation' . $i, $value);
    $i++;
    }

    It will save your checkboxes in the database, by accreditation1, accreditation2 and so on..

  • avatar
    Last edited:
    03/31/11
    1:04pm
    Denzel Chia says:

    Hi,

    Using name="accreditations[]" for your checkbox is correct.
    This will post the values as an array to your save post meta function, and it will be saved as an array.
    To avoid PHP error, i assume that you use
    $accreditation = get_post_meta($post_id,'accreditation',true);
    to retrieve your post meta value.

    You need to use foreach to loop the array before assigning back to your checkbox to determine which box to check. This also applies to your single.php where you are echoing out the values!

    I am not sure what are your codes, therefore I can only assume and give you examples.

    For your form it should be something like this, since your post meta is an array value.


    <?php
    global $post;
    $post_id = $post->ID;
    $acc = array();
    $acc = get_post_meta($post_id,'accreditations',true);

    echo'<label for="accreditations">' . __("Accreditations", 'myplugin_textdomain' ) . '</label>';

    echo '<input type="checkbox" name="accreditations" value="aaa_tourism"';

    foreach($acc as $accreditations){
    if($accreditations == 'aaa_tourism'){echo 'checked';}else{echo '';};
    }

    echo' /> AAA Tourism';

    echo '<input type="checkbox" name="accreditations" value="accredited_tourism_business_australia"';

    foreach($acc as $accreditations){
    if($accreditations == 'accredited_tourism_business_australia'){echo 'checked';}else{echo '';};
    }

    echo '/> Accredited Tourism Business Australia ';

    echo '<input type="checkbox" name="accreditations" value="environmentally_friendly"';

    foreach($acc as $accreditations){
    if($accreditations == 'environmentally_friendly'){echo 'checked';}else{echo '';};
    }

    echo '/> Environmentally Friendly';

    }
    ?>



    And for your single.php, it should be something like this.


    <?php

    //for echo out accreditations
    //assuming post meta key is accreditations

    global $post;
    $post_id = $post->ID;
    $accreditations = get_post_meta($post_id,'accreditations',true);
    $template_url = get_bloginfo('template_url');

    foreach($accreditations as $accres){

    switch ($accres) {
    case "aaa_tourism":
    echo "<img src='$template_url/images/logo-aaa-tourism.png' title='AAA Tourism'>";
    break;
    case "accredited_tourism_business_australia":
    echo "<img src='$template_url/images/logo-atba.png' title='Accredited Tourism Business Australia'>"; break;
    case "environmentally_friendly":
    echo "<img src='$template_url/images/logo-environmentally-friendly.png' title='Environmentally Friendly'>";
    break;
    }
    }
    ?>


    Thanks.

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.