contact form PHP redirect is not working

前端 未结 7 393
迷失自我
迷失自我 2021-01-20 01:42

I im trying to redirect to my homepage after submitting a message on my contact form, the form sends the email but I get this message:

Array
(
    [name] =&g         


        
相关标签:
7条回答
  • 2021-01-20 01:48

    This is very common problem in php. some ways to sort out:

    1. use on top of the page.

    2. check if you accidentally add some white spaces before the php open tag or after the php closed tag.

    3. if still not solve, use java-script window.location instead of header.

    Hope it help. Happy coding!!

    0 讨论(0)
  • 2021-01-20 01:52

    You can't output anything to the screen before redirecting.

    Remove all your echo'es and print_r's and you should be fine.

    EDIT:

    Also as @jhonraymos mentioned, be sure to use header() properly. Maybe you changed this to hide your actual page you're redirecting to. Either add a local file with correct path definitions or if redirecting to an other domain, you need to define the full url. See Uniform resource locator if in doubt.

    Another EDIT:

    I see you updated your question. Stop trying indian magic, such as

    if ($success){
        print "<meta http-equiv=\"refresh\" content=\"0;URL=YOUR_PAGE_HERE.html\">";
    

    Just accept the fact not to output ANYTHING to the screen before headers() and your soul is safe forever. This is not the place to mix http-meta in. PHP can do this just fine.

    This might sound as a limitation first, but believe me, it isn't. It's a blessing.

    0 讨论(0)
  • 2021-01-20 01:56

    You get this warning because you output to the screen your variables' values and redirecting with header after that.

    Headers cannot be sent after your print something (echo , print_r ...). In order to fix it , follow the next code:

    $name = $_POST['name']; //no echo 
    $company = $_POST['company'];//no echo 
    $email =  $_POST['email'];//no echo 
    $content = $_POST['content'];//no echo 
    
    
    
    
    $mail_to = 'info@webelite.se';
    $subject = 'Lilla form'.$name;
    
    
    
    $body_message = 'From: '. $name . "\n"; 
    $body_message .= 'company: '. $company . "\n";
    $body_message .= 'E-mail: '. $email ."\n";
    $body_message .= 'Message: '. $content;
    
    
    
    $headers = 'From: '. $mail_name . "\r\n";
    $headers .= 'Reply-To: '. $email ."\r\n";
    
    
    
    $success = mail($mail_to, $subject, $body_message, $headers);
    
    
    
    //echo "<pre>";
    //print_r($_POST);
    
    0 讨论(0)
  • 2021-01-20 02:00

    Headers are used to communicate with your client's browser. They're like little commands that the browser will perform when received. When you output any data (text,numbers,whatever), you're client's browser will print that data. After that, the browser will no longer be interested in any headers you send.

    The header() function is a function used to send custom headers. So when this function is called, headers are sent out to your client's browser.

    Now you have a very brief understanding of what it is you're actually trying to do, you should be able to see where your problem lies.

    You are outputting other data before sending those custom headers. This is what's triggering the error.

    So this:

    echo "<pre>";
    print_r($_POST);
    

    Should not be before this:

    header('Location:mydomain');
    
    0 讨论(0)
  • 2021-01-20 02:01

    this is error

    print_r($_POST);
    
    header('Location:mydomain');
    

    You are printing something before the header("location: mydomain.com")

    0 讨论(0)
  • 2021-01-20 02:01

    yes, you are not supposed to echo, print anything before header try placing this at the top of your page:

    <?php ob_start(); ?>
    

    then at the bottom of the page place :

    <?php ob_end_flush(); ?>
    
    0 讨论(0)
提交回复
热议问题