$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to redirect user to a page after form submission|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to redirect user to a page after form submission

matteradmin11PV0评论

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');

Post a comment

comment list (0)

  1. No comments so far