skip to Main Content

I have a relatively simple PHP form. When I’m using print_r($_POST); I’m not receiving anything back. Just this result:

`Array
  (
  )
  1`

My form is pretty simple:

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

The goal of the form is to simply post and then redirect to a page depending on the value received from the input. Any thoughts?

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

Answers


  1. I just ran your script,(xampp , php 7.1.26 , mac Mojave) and it prints

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

    I added <?php print_r($_POST); ?> on top of the tag.( you might have done the same) and for me it worked as expected.

    silly but did just wanted to know whether you submitted the form? if not it will show an empty array

    Login or Signup to reply.
  2. Your script post to the server just fine, but before hitting the print_r($_POST) it gets redirect to the same location. so print_r($_POST) was not executed . Please refer below image for additional info. There are two requests , one is post with 302 redirect. I suspect there maybe a javascript redirect involved.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search