mardi 1 novembre 2016

Checkbox group array always empty

I have a really irritating problem with a form I'm working on. There are two inputs:

  • a text input field to enter email address (no problem)
  • a checkbox group generated by a foreach'es with info from the database (leaves empty input)

The whole form looks like this:

<form role="form" id="contactForm" class="contact-form" data-toggle="validator" class="shake">`

$post_type = 'education';
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );

foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );

foreach( $terms as $term ) : ?>

        <h2><?php echo $term->name; ?></h2>

    <?php
    $args = array(
            'post_type' => $post_type,
            'posts_per_page' => -1,  //show all posts
            'tax_query' => array(
                array(
                    'taxonomy' => $taxonomy,
                    'field' => 'slug',
                    'terms' => $term->slug,
                )
            )

        );
    $posts = new WP_Query($args);


    if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
                <h4><?php the_title();?></h4>
     <div class="form-group">
            <div class="controls">
            <?php $fil = get_field('fil');
            if( !empty($fil) ): ?>
              <input type="checkbox" value="test" name="download_file[]" class="form-control">
              <?php endif; ?>
              <div class="help-block with-errors"></div>
            </div>
        </div>

    <?php endwhile; endif; ?>
    <div class="clear"></div>
    <br />
    <hr>
    <br />

<?php endforeach;

endforeach;  ?>
          <div class="form-group">
            <div class="controls">
              <input type="email" class="email form-control" name="email" placeholder="Email" id="email" required data-error="Please enter your email">
              <div class="help-block with-errors"></div>
            </div>
          </div>

          <button type="submit" id="submit" class="btn btn-success"></i> Send Message</button>
          <div id="msgSubmit" class="h3 text-center hidden"></div> 
          <div class="clearfix"></div>   

        </form>  

As soon as the button gets clicked, the content from the form gets sent as an email. The email address from the form is visible, but the download_file part is always empty, no matter how many checkboxes I check.

I use the following code to see the output from the form:

<table>
<?php 


foreach ($_POST as $key => $value) {
    echo "<tr>";
    echo "<td>";
    echo $key;
    echo "</td>";
    echo "<td>";
    echo $value;
    echo "</td>";
    echo "</tr>";
}


?>
</table>

and this outputs [object Object] for each checkbox that is checked.

Does anyone know why this happens? Can it be something with the foreach'es that ends before the form ends?

Many thanks in advance!




Aucun commentaire:

Enregistrer un commentaire