Home > Blockchain >  GravityForms Dynamic Confirmation Redirects
GravityForms Dynamic Confirmation Redirects

Time:02-02

I have set up a gravity form with a single line text field. I am using it to check for zip codes and cities. If someone types in the correct zip code it will redirect to a specific page. Also if someone types in a zip code or city and there is no matching value it will also redirect to a certain page. I have tried this code. But I can not seem to get it to work correctly. It works if I have one number but if I add multiple numbers separated by a comma it does not work. Please help if possible.

    <?php
function de_gforms_confirmation_dynamic_redirect( $confirmation, $form, $entry, $ajax ) {
    if ( $form['id'] == '38' ) {
        if ( $entry['1'] == '89103,80205,91030' ) {
        $confirmation = array( 'redirect' => 'https://google.com' );
        } else if ( $entry['1'] == '90210,89554,90454' ) {
        $confirmation = array( 'redirect' => 'https://yahoo.com' );
        } else if ( $entry['1'] == '56678,89004,78896' ) {
        $confirmation = array( 'redirect' => 'https://test.com' );
        }
    }

    return $confirmation;
}

add_filter( 'gform_confirmation', 'de_gforms_confirmation_dynamic_redirect', 10, 4 );
?>

CodePudding user response:

You need to use the or operator "||" instead of the array of zipcodes

<?php
    function de_gforms_confirmation_dynamic_redirect( $confirmation, $form, $entry, $ajax ) {
        if ( $form['id'] == '38' ) {
            if ( $entry['1'] == '89103' || $entry['1'] == '80205' || $entry['1'] == '91030' ) {
                $confirmation = array( 'redirect' => 'https://google.com' );
            } else if ( $entry['1'] == '90210' ||  $entry['1'] == '89554' ||  $entry['1'] == '90454' ) {
                $confirmation = array( 'redirect' => 'https://yahoo.com' );
            } else if ( $entry['1'] == '56678' ||  $entry['1'] == '89004' ||  $entry['1'] == '78896' ) {
                $confirmation = array( 'redirect' => 'https://test.com' );
            }
        }
    
        return $confirmation;
    }

CodePudding user response:

There's a lot of ways you could do this. I would probably try to turn your zip codes/redirect into an associative array (https://wtools.io/convert-csv-to-php-array) that you could loop through like this:

function de_gforms_confirmation_dynamic_redirect( $confirmation, $form, $entry, $ajax ) {
        $zip_group = array("1234","2345","3456");
        $count_group = count($zip_group);
        for($i=0;$i<$count_group;$i  ){
            $redirect[$i]='https://google.com';
        }
        $zip_condition = array_combine( $zip_group , $redirect );
        
        if( $form['id'] == '38' ) {
            foreach($zip_condition as $zip => $condition) { 
                if ( $entry['1'] == $zip ) {
                   $confirmation = array( 'redirect' => $condition );
                } 
            }
        }
        return $confirmation;
    }
  •  Tags:  
  • Related