redirect to a thank you page

Discussion in 'PHP' started by timmckeown, Mar 27, 2007.

  1. #1
    Hi I am having trouble getting redirecting to a thank you page after a simple html form has been completed. Can anyone help me fix this?

    Thanks!
    Tim

    <?php

    // set up mail details
    $mail_to = "timm@think-in.co.uk";
    $mail_from = stripslashes($HTTP_POST_VARS['Email']);
    $mail_subject = "PRINT - IN - WEBSITE FORM";
    $mail_headers = "FROM: $mail_from\r\n";
    // grab details from http post array and iterate through each to build string
    $mail_body = "";
    $mail_body .= "PRINT-IN - WEBSITE FORM\n";

    while(list($key, $value) = each($HTTP_POST_VARS))
    {
    $mail_body .= "\n$key = $value\n";
    }

    //create mail footer message
    $mail_footer = "\n\nMailer enquiry for THINK-IN";

    //concatinate mail_body and mail_footer
    $mail_body = $mail_body . $mail_footer;

    // send mail
    if (mail($mail_to, $mail_subject, $mail_body, $mail_headers)) {
    //echo ("<p>Enquiry Email Sent<p>");
    $success = 1;
    } else {
    //echo ("<p>Error On Sending Mail</p>");
    $success = 0;
    }
    ?>

    <TABLE width="300" border="0" cellspacing="0" cellpadding="0" >
    <TR>
    <TD colspan="2" class=std>
    <?php
    //create messsage besed on whether mail was sent or not
    if ($success == 1)
    echo "<script>window.location.href(\"http://www.example.com\");</script>";
    else
    echo ("There has been an error sending your details please click back and try again</a>");
    ?>
     
    timmckeown, Mar 27, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Replace:
    
    //echo ("<p>Enquiry Email Sent<p>");
    
    PHP:
    with
    
    header('Location: http://www.example.com/thank-you.html');
    exit();
    
    PHP:
     
    nico_swd, Mar 27, 2007 IP
  3. timmckeown

    timmckeown Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi, thanks for looking at this, the form sends but I recieve this message...

    Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/print-in.com/httpdocs/Library/send_details.php:4) in /var/www/vhosts/print-in.com/httpdocs/Library/send_details.php on line 78
     
    timmckeown, Mar 27, 2007 IP
  4. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That's because headers (like redirect headers) have to come before any other text.

    Put this as your first line of PHP code:
    ob_start();
    PHP:
     
    Robert Plank, Mar 27, 2007 IP
  5. timmckeown

    timmckeown Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ok here is the code with those two amends, still get the warning message though...thanks for your help here.

    <HTML>
    <HEAD>
    <TITLE>WEBSITE FORM</TITLE>
    <script type="text/JavaScript">
    <!--
    function MM_goToURL() { //v3.0
    var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
    for (i=0; i<(args.length-1); i+=2) eval(args+".location='"+args[i+1]+"'");
    }
    //-->
    </script>
    <STYLE type="text/css">
    <!--
    BODY
    {
    }
    A
    {
    COLOR: #666666;
    TEXT-DECORATION: none;
    }
    A:hover
    {
    TEXT-DECORATION: underline;
    }
    .small
    {
    FONT-SIZE: 8pt;
    FONT-FAMILY: Arial, Helvetica, sans-serif;
    }
    .std
    {
    FONT-SIZE: 8pt;
    FONT-FAMILY: Arial, Helvetica, sans-serif;
    }
    .heading
    {
    FONT-SIZE: 8pt;
    FONT-FAMILY: Tahoma;
    FONT-WEIGHT: BOLD;
    COLOR:#666666;
    }
    .bigheading
    {
    FONT-SIZE: 8pt;
    FONT-FAMILY: Tahoma;
    FONT-WEIGHT: BOLD;
    COLOR:#666666;
    }
    -->
    </STYLE>
    </HEAD>
    <BODY onLoad="MM_goToURL('http://www.print-in.com/index.php');return document.MM_returnValue">
    <?php
    ob_start();
    // set up mail details
    $mail_to = "timm@think-in.co.uk";
    $mail_from = stripslashes($HTTP_POST_VARS['Email']);
    $mail_subject = "PRINT - IN - WEBSITE FORM";
    $mail_headers = "FROM: $mail_from\r\n";
    // grab details from http post array and iterate through each to build string
    $mail_body = "";
    $mail_body .= "PRINT-IN - WEBSITE FORM\n";

    while(list($key, $value) = each($HTTP_POST_VARS))
    {
    $mail_body .= "\n$key = $value\n";
    }

    //create mail footer message
    $mail_footer = "\n\nMailer enquiry for THINK-IN";

    //concatinate mail_body and mail_footer
    $mail_body = $mail_body . $mail_footer;

    // send mail
    if (mail($mail_to, $mail_subject, $mail_body, $mail_headers)) {
    header('Location: http://www.print-in.com/thank-you.html');
    exit();
    $success = 1;
    } else {
    //echo ("<p>Error On Sending Mail</p>");
    $success = 0;
    }
    ?>

    <TABLE width="300" border="0" cellspacing="0" cellpadding="0" >
    <TR>
    <TD colspan="2" class=std>
    <?php
    //create messsage besed on whether mail was sent or not
    if ($success == 1)
    echo ("<script>window.location.href(\"http://www.example.com\");</script>");
    else
    echo ("There has been an error sending your details please click back and try again</a>");
    ?>
    <BR> </TD>
    </TR>
    </TABLE>

    </BODY>
    </HTML>
     
    timmckeown, Mar 27, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Move ALL the PHP code above your HTML code, so that there is NOTHING before.
     
    nico_swd, Mar 27, 2007 IP
  7. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Either have:

    <?php ob_start(); ?>
    then HTML code
    then the PHP code

    or...

    all the PHP code
    then the HTML code
     
    Robert Plank, Mar 27, 2007 IP
  8. timmckeown

    timmckeown Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Works perfectly. Thanks for the help, awesome.
     
    timmckeown, Mar 27, 2007 IP