doujiunai2169 2017-01-28 18:15
浏览 42
已采纳

PHP $ _POST没有拾取选择输入

Right, I know this has been asked many times before but the truth is there was no definitive answer that I could use from all those questions.

My $_POST array never seems to pick up my select element in my form when I have clearly chosen a value for that form.

I have a contact form on a WordPress theme as such:

 <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>

This then gets processed in contact.php:

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

    if (!defined("PHP_EOL")) define("PHP_EOL", "
");

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

When I submit the form I get the following error:

Notice: Undefined index: event_select in /homepages/5/d586424128/htdocs/wp-content/themes/NativeChurch/mail/contact.php on line 13

I just can't get my head around why the select field is not being sent to the $_POST variable and I have definitely have values for each of the options in the select box because my source for the select looks like this:

<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条回答 默认 最新

  • dongpang4470 2017-01-28 19:03
    关注

    After some digging around it seems like there is some JavaScript getting triggered on the class contact-form-native on my form. After doing a search through all the theme files I found the following code:

    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;
                });
            });
        }
    

    I just had to add event_select: $('#event_select').val(), to the post options above for the form to pick it up. So it was JavaScript that was actually submitting the form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭