How to redirect user to a page after form submission

admin2025-06-04  1

I have three dropdown filters on the front site to select country, territory and region and need to redirect the user to the country/territory/region page depending upon the filter values on click of Submit button.

FYI, I have pages with the same slug.eg- example/country-name/territory-name/region-name

I am trying to use wp_redirect( $final_url ); exit; but it is throwing a warning-

Warning: Cannot modify header information - headers already sent by (output started at E:\path\to\root-folder\wp-includes\class.wp-styles.php:154) in E:\path\to\root-folder\wp-includes\pluggable.php on line 1121

Please help.

Can someone suggest me some some better way?

//Edit- here is my code

if(isset($_POST['submit']) and $_POST['action']=='findurl')
    {
        //Result $POST Array ( [countrySelect] => 1 [territorySelect] => 1 [regionSelect] => 2 )
        if (isset($_POST['countrySelect'])){

            $rows_country_url = $wpdb->get_results("select url from country where id=".$_POST['countrySelect']."");
        }

        if (isset($_POST['territorySelect'])){

            $rows_territory_url = $wpdb->get_results("select url from territory where id=".$_POST['territorySelect']."");
        }

        if (isset($_POST['regionSelect'])){

            $rows_region_url = $wpdb->get_results("select url from region where id=".$_POST['regionSelect']."");
        }


        if($rows_country_url[0]->url !=''){
        $final_url = home_url('/').$rows_country_url[0]->url;}

        if($rows_territory_url[0]->url !=''){
        $final_url = $final_url.'/'.$rows_territory_url[0]->url;}

        if($rows_region_url[0]->url !=''){
        $final_url = $final_url.'/'.$rows_region_url[0]->url;}

        echo $final_url;

        if($final_url!=''){ 
        wp_redirect( home_url() ); exit;
        echo "not blank";
        }else{ echo "Please select values";}
    }

I have three dropdown filters on the front site to select country, territory and region and need to redirect the user to the country/territory/region page depending upon the filter values on click of Submit button.

FYI, I have pages with the same slug.eg- example/country-name/territory-name/region-name

I am trying to use wp_redirect( $final_url ); exit; but it is throwing a warning-

Warning: Cannot modify header information - headers already sent by (output started at E:\path\to\root-folder\wp-includes\class.wp-styles.php:154) in E:\path\to\root-folder\wp-includes\pluggable.php on line 1121

Please help.

Can someone suggest me some some better way?

//Edit- here is my code

if(isset($_POST['submit']) and $_POST['action']=='findurl')
    {
        //Result $POST Array ( [countrySelect] => 1 [territorySelect] => 1 [regionSelect] => 2 )
        if (isset($_POST['countrySelect'])){

            $rows_country_url = $wpdb->get_results("select url from country where id=".$_POST['countrySelect']."");
        }

        if (isset($_POST['territorySelect'])){

            $rows_territory_url = $wpdb->get_results("select url from territory where id=".$_POST['territorySelect']."");
        }

        if (isset($_POST['regionSelect'])){

            $rows_region_url = $wpdb->get_results("select url from region where id=".$_POST['regionSelect']."");
        }


        if($rows_country_url[0]->url !=''){
        $final_url = home_url('/').$rows_country_url[0]->url;}

        if($rows_territory_url[0]->url !=''){
        $final_url = $final_url.'/'.$rows_territory_url[0]->url;}

        if($rows_region_url[0]->url !=''){
        $final_url = $final_url.'/'.$rows_region_url[0]->url;}

        echo $final_url;

        if($final_url!=''){ 
        wp_redirect( home_url() ); exit;
        echo "not blank";
        }else{ echo "Please select values";}
    }
Share Improve this question edited Jun 27, 2014 at 8:53 user2443437 asked Jun 27, 2014 at 8:29 user2443437user2443437 211 gold badge1 silver badge3 bronze badges 2
  • Where are you using wp_redirect? You can't output any HTML and then call wp_redirect. It must be called before you output any content to the browser. Show some code. – Todd Rowan Commented Jun 27, 2014 at 8:35
  • Hi Todd, I have edited my question to show the code. Please have a look. – user2443437 Commented Jun 27, 2014 at 8:54
Add a comment  | 

2 Answers 2

Reset to default 1

Please make sure these is no data output or blank spaces above wp_redirect( $final_url ); exit; otherwise this warning will always appear.

Also optionally you can use

<?php
//Php code 
 ?>
<script type="text/javascript">
      document.location.href="http://example";
</script>
<?php
//php code
?>

My solution is.

  1. Create a function for javascript redirect.

    function rm_redirect($url){ $string = ''; $string .= 'window.location = "' . $url . '"'; $string .= ''; echo $string; }

  2. Call this function whenever you want like this.

    rm_redirect('http://example');

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749003086a315530.html

最新回复(0)