PHP $ _POST没有拾取选择输入

时间:2017-01-28 18:15:09

标签: php html wordpress forms select

是的,我知道之前已经多次询问过这个问题,但事实是,在所有这些问题中我都没有明确的答案。

当我明确为该表单选择一个值时,我的$_POST数组似乎永远不会在我的表单中选择我的选择元素。

我在WordPress主题上有一个联系表格:

 <form method="post" id="contactform" name="contactform" class="contact-form-native" action="<?php echo get_template_directory_uri() ?>/mail/contact.php">
                        <div class="col-md-6 margin-15">
                            <div class="form-group">
                                <input type="text" id="name" name="name"  class="form-control input-lg" placeholder="<?php _e('Name*','framework'); ?>">
                            </div>
                            <div class="form-group">
                                <input type="email" id="email" name="email"  class="form-control input-lg" placeholder="<?php _e('Email*','framework'); ?>">
                            </div>
                            <div class="form-group">
                                <input type="text" id="phone" name="phone" class="form-control input-lg" placeholder="<?php _e('Phone','framework'); ?>">
                                <input type ="hidden" name ="image_path" id="image_path" value ="<?php echo get_template_directory_uri() ?>">
                            <input id="admin_email" name="admin_email" type="hidden" value ="<?php echo $admin_email; ?>">
                            <input id="subject" name="subject" type="hidden" value ="<?php echo $subject_email; ?>">
                            </div>


                            <div class="form-group">
                                <select id="event_select" name="event_select" class="form-control">

                                    <option disabled selected value> -- select the event -- </option>

                        <?php
                                        $args = array(
                                            // Arguments for your query.
                                             'post_type' => 'event',

                                             'meta_query' => array(
                                                    'start_date_clause' => array(
                                                      'key' => 'imic_event_start_dt',
                                                      'value' => date("Y-m-d", strtotime('first day of January '.date('Y-m-d') )),
                                                      'compare' => '>'
                                                    ),
                                                ),

                                            'orderby' => 'start_date_clause',
                                            'order' => 'ASC'
                                        );

                                        // Custom query.
                                        $query = new WP_Query( $args );

                                        // Check that we have query results.
                                        if ( $query->have_posts() ) {

                                            // Start looping over the query results.
                                            $count = 0;
                                            while ( $query->have_posts() ) {

                                                $query->the_post();
                                                echo '<option value="' . $count . '">' . date('jS M', strtotime(get_post_meta(get_the_ID(),'imic_event_start_dt',true))) . " - " . date('jS M', strtotime(get_post_meta(get_the_ID(),'imic_event_end_dt',true))) .  "</option>";

                                                $count++;
                                            }

                                        }


                                        ?>

                                </select>
                            </div>

                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <textarea cols="6" rows="7" id="comments" name="comments" class="form-control input-lg" placeholder="<?php _e('Message','framework'); ?>"></textarea>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <input id="submit" name="submit" type="submit" class="btn btn-primary btn-lg pull-right" value="<?php _e('Submit now!','framework'); ?>">
                        </div>
                    </form>

然后在contact.php中处理:

    // - grab wp load, wherever it's hiding -
    include "../../../../wp-config.php";
    if(!$_POST) exit;

    if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

    $event_select  = $_POST['event_select'];
    $name     = $_POST['name'];
    $email    = $_POST['email'];
    $phone    = $_POST['phone'];
    $subject     = $_POST['subject'];

当我提交表单时,我收到以下错误:

  

注意:未定义的索引:event_select in   /homepages/5/d586424128/htdocs/wp-content/themes/NativeChurch/mail/contact.php   第13行

我无法理解为什么没有将select字段发送到$ _POST变量,并且我肯定有选择框中每个选项的值,因为我的select源代码如下所示:

<select id="event_select" name="event_select" class="form-control">                                     
    <option disabled="" selected="" value=""> -- select the event -- </option>
    <option value="0">16th Feb - 23rd Feb</option>
    <option value="1">27th Feb - 5th Mar</option>
    <option value="2">20th Mar - 26th Mar</option>
    <option value="3">20th Apr - 26th Apr</option>
    <option value="4">8th May - 14th May</option>
    <option value="5">15th Jun - 25th Jun</option>
    <option value="6">7th Aug - 14th Aug</option>
    <option value="7">20th Sep - 25th Sep</option>
    <option value="8">16th Oct - 23rd Oct</option>                              
</select>

1 个答案:

答案 0 :(得分:0)

经过一些挖掘后,似乎在我的表单上的类contact-form-native上触发了一些JavaScript。在搜索完所有主题文件后,我发现了以下代码:

NATIVE.ContactForm = function() {
        $('.contact-form-native').each(function() {
            var formInstance = $(this);
            formInstance.submit(function() {
                var action = $(this).attr('action');
                $("#message").slideUp(750, function() {
                    $('#message').hide();
                    $('#submit')
                            .after('<img src="' + $('#image_path').val() + '/images/assets/ajax-loader.gif" class="loader" />')
                            .attr('disabled', 'disabled');
                    $.post(action, {
                        name: $('#name').val(),
                        email: $('#email').val(),
                        phone: $('#phone').val(),
                        comments: $('#comments').val(),
                        subject: $('#subject').val(),
                        admin_email: $('#admin_email').val()
                    },
                            function(data) {
                                document.getElementById('message').innerHTML = data;
                                $('#message').slideDown('slow');
                                $('.contact-form-native img.loader').fadeOut('slow', function() {
                                    $(this).remove()
                                });
                                $('#submit').removeAttr('disabled');
                                if (data.match('success') != null)
                                    $('.contact-form-native').slideUp('slow');
                            }
                    );
                });
                return false;
            });
        });
    }

我只需要将event_select: $('#event_select').val(),添加到上面的帖子选项中,然后选择表单即可。所以实际上提交表单的是JavaScript。