我的PHP表单未从$ _POST返回任何内容

时间:2019-04-07 19:13:56

标签: php html forms

我有一个相对简单的PHP表单。当我使用print_r($_POST);时,我什么也没收到。只是这个结果:

`Array
  (
  )
  1`

我的表单非常简单:

<form action="" method="post" enctype="multipart/form-data" class="zip-<?php echo $atts['class']; ?>">
        <div class="row ">
            <div class="<?php echo $column1; ?>" <?php echo $inlineStyle ?>>
                <div class="lds-facebook hide"><div></div><div></div><div></div><div></div><div></div></div>
                <input type="text" name="zip" class="zip">
            </div>
            <div class="<?php echo $column2 ?>">
                <button type="submit" class="submit" name="submit">
                  <?php echo $buttonValue; ?>
                  <i class='fa fa-caret-right' aria-hidden='true'></i>
                </button>
            </div>
        </div>
    </form>

表单的目标是简单地发布,然后根据从输入接收的值重定向到页面。有什么想法吗?

public static function find_zip_widget( $atts, $content = null ) { 
    ini_set('display_errors', '1');

    extract(shortcode_atts(array(
    'class' => 'class'
), $atts));

    // Set classes for rows
    if($atts['class'] == 'home'){
        $column1 = 'col-md-6 col-12 my-auto text-center border-underline';
        $column2 = 'col-md-6 col-12';
        $buttonValue = 'FIND HELP RIGHT NOW';
        $inlineStyle = '';
    }

    if($atts['class'] == 'widget'){
        $column1 = 'col-md-5 col-5 offset-2 offset-md-0 my-auto';
        $column2 = 'col-md-7 col-5 my-auto';
        $buttonValue = 'SUBMIT';
        $inlineStyle = 'style="border-bottom: 3px solid #9EA2A4 !important;min-height: 30px;"';
    }

    ?>
    <form action="" method="post" enctype="multipart/form-data" class="zip-<?php echo $atts['class']; ?>">
        <div class="row ">
            <div class="<?php echo $column1; ?>" <?php echo $inlineStyle ?>>
                <div class="lds-facebook hide"><div></div><div></div><div></div><div></div><div></div></div>
                <input type="text" name="zip" class="zip">
            </div>
            <div class="<?php echo $column2 ?>">
                <button type="submit" class="submit" name="submit">
                  <?php echo $buttonValue; ?>
                  <i class='fa fa-caret-right' aria-hidden='true'></i>
                </button>
            </div>
        </div>
    </form>

    <?php    
        echo "<pre>";
        echo "Before if: ";
        echo  print_r($_POST); 
        echo "</pre>";
    ?>

    <?php 
        $states = get_categories( array(
        'orderby' => 'name',
        'order'   => 'ASC',
        'hide_empty' => false,
        'taxonomy' => 'endeavors_location_state'
        ) );


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

            echo "<pre>";
            echo "After if: ";
            echo  print_r($_POST);
            echo "</pre>";

            echo '<br> <br> PostalZip: ' . $postalZip;

            ///REDIRECT FOR CITY, STATE, or other/////
            $ch = curl_init(); 
            curl_setopt($ch, CURLOPT_URL, "https://maps.googleapis.com/maps/api/geocode/json?address='.$postalZip.'&key=AIzaSyBX_0qZmGBtiHrZMcjZfv6yL7NAbLiwnjc");
            // curl_setopt($ch, CURLOPT_URL, "https://maps.googleapis.com/maps/api/geocode/json?address=78210&key=AIzaSyBX_0qZmGBtiHrZMcjZfv6yL7NAbLiwnjc");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);

            curl_close($ch);
            $jsonResults = json_decode($output, true);



            $cityName = $jsonResults['results'][0]['address_components'][1]['long_name'];
            $stateName = $jsonResults['results'][0]['address_components'][3]['long_name'];


            $cityName = strtolower(str_replace(" ","-",$cityName));
            $stateName = strtolower(str_replace(" ","-",$stateName));

            $args = array(
                'post_type' => 'endeavors_locations',
                'post_status' => 'publish',
              'numberposts' => -1
            );

            $cityFound = 0;
            $allCityLocations = get_posts( $args );
            foreach ($allCityLocations as $location) {
                $eachCity = strtolower(str_replace(" ","-",$location->post_name));
                echo $eachCity;

                if ($location->post_name === $cityName) {
                    $cityFound = 1;
                    echo $cityFound;
                }
            }

            if ($cityFound === 1) {
                wp_safe_redirect('/locations/'.$cityName);
                } else {
                foreach ($states as $state) {
                    if ($state->slug === $stateName) {
                         $stateFound = 1;
                    }
                }
                if ($stateFound === 1) {
                wp_safe_redirect('/state/'.$stateName);
                } else {
                    wp_safe_redirect('/all-locations/');
                }
            }
        }
    ///END OF REDIRECT FOR CITY, STATE, or other/////
    } //end function

2 个答案:

答案 0 :(得分:2)

您的脚本发布到服务器很好,但是在到达print_r($ _ POST)之前,它已重定向到相同的位置。因此未执行print_r($ _ POST)。请参考下图以获取更多信息。有两个请求,一个是具有302重定向的帖子。我怀疑可能涉及到JavaScript重定向。

enter image description here

答案 1 :(得分:0)

我刚刚运行了您的脚本(xampp,php 7.1.26,mac Mojave),它会打印

Array ( [zip] => testvalue [submit] => )

我在标签的顶部添加了<?php print_r($_POST); ?>。(您可能也做过同样的事情)对我来说,它按预期运行。

很傻,但是只是想知道您是否提交了表格?如果没有,它将显示一个空数组