0

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
4
  • Please add the relevant parts of the PHP script. Commented Apr 7, 2019 at 19:21
  • I added the entire shortcode function. Commented Apr 7, 2019 at 19:25
  • 1
    if i may can you copy the html code from the developer tools and make a new php file and test it by itself that could shed some light Commented Apr 7, 2019 at 19:51
  • Done and done.....still not seeing anything new. Commented Apr 7, 2019 at 20:45

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, so I can definitely see my value submitting now. So why wouldn't my if (isset($_POST['submit_value'])) { be firing?
That is something you need to investigate. Try adding a die() statement after print_r($_POST) to stop executing further php and rendering the HTML and let us know what you get :)
I added the die() and that seemed to show me the array of $_POST. So that's good. Looks like it is getting into the if isset too. Now I have a new problem to solve. Thanks everyone!!!
@Lz430 looking at the isset($_POST['submit']) portion you are calling wp_safe_redirect, meaning $_POST will be empty upon redirecting.
0

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

5 Comments

i might think the post variable is altered somehow. could be using a framework or something. i did exactly what you did and i didn't make any sense of it
Thank you both. I'm on WPengine running php 7.2. I don't believe there are any frameworks. I guess for sanity's sake, there's nothing wrong with my form markup or anything else if you guys are getting it to print, right?
to be honest i spotted a mark up thing that is not related to the issue <i class='fa fa-caret-right' aria-hidden='true'></i> used single quote :D
Talal, this is related to wordpress maybe. @Lz430 can you try the following solutions 1. Change the input name from 'zip ' to something unique 'yournamezip' just to check 2. change the action <form action="<?php the_permalink(); ?>" and see this helps
Ok, I've done both and I'm still getting nothing. I've even tried echo print_r($_POST['geoZip']); For reference the URL is here: endeavorsdev1.wpengine.com/all-locations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.