1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

response.redirect in php

Discussion in 'PHP' started by red_fiesta, Jun 1, 2007.

  1. #1
    is there a command to do this.

    so for example you can have a php that does a store to the database and then goes somewhere else..

    Thanks
     
    red_fiesta, Jun 1, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    nico_swd, Jun 1, 2007 IP
  3. red_fiesta

    red_fiesta Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    well i did and i am using the code

    <?php
    
    
    header("Location: http://www..../thanks.htm");
    
    ?>
    
    <?php
    
    $name = $_POST["aname"];
    $fromadd  = $_POST["aemail"];
    $phone  = $_POST["atelephone"];
    $jtitle= $_POST["ajtitle"];
    $company= $_POST["acompany"];
     
     
     $to = "...@yahoo.com";
    $subject = "Brochure Request";
    $body = "This is a request from the website for a .....,\n\nThe requst has come from ".$name."\n\nTheir phone number is ".$phone."\n\nThey are from the company ".$company."\n\nThier Job Title is ".$jtitle;
    $headers = "From: ".$fromadd."\r\n" .
        "X-Mailer: php";
    if (mail($to, $subject, $body, $headers)) {
     echo("<p>Message sent!</p>");
     } else {
      echo("<p>Message delivery failed...</p>");
    }
    
    
    ?>
    Code (markup):
    but it just shows message sent... i want it to send the email and goto thanks.htm

    what is wrong?
     
    red_fiesta, Jun 1, 2007 IP
  4. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #4
    
    header("Location: http://www..../thanks.htm");
    exit();  // you need to exit after a Location header is sent
    
    PHP:
     
    mrmonster, Jun 1, 2007 IP
  5. red_fiesta

    red_fiesta Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hmm

    if i use that the page doesnt send the email or redirect.. is just blank

    <?php
    
    
    header("Location: http://www.....co.uk/contactthanks.html");
    
    exit(); 
    
    ?>
    
    <?php
    
    $name = $_POST["name"];
    $fromadd  = $_POST["email"];
    $phone  = $_POST["telephone"];
    $jtitle= $_POST["jtitle"];
    $company= $_POST["company"];
     
     
     $to = "...@yahoo.com";
    $subject = "Brochure Request";
    $body = "This is a request from the website for a ...Brochure,\n\nThe requst has come from ".$name."\n\nTheir phone number is ".$phone."\n\nThey are from the company ".$company."\n\nThier Job Title is ".$jtitle;
    $headers = "From: ".$fromadd."\r\n" .
        "X-Mailer: php";
    if (mail($to, $subject, $body, $headers)) {
     echo("<p>Message sent!</p>");
     } else {
      echo("<p>Message delivery failed...</p>");
    }
    
    
    ?>
    
    
    Code (markup):
     
    red_fiesta, Jun 1, 2007 IP
  6. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #6
    1. You cannot have any output to the page before header("Location: ") is called, it has to be the first thing thats sent to the browser.

    2. exit() means just that, when you're driving and you take exit 123 on the highway, you won't make it to exit 124 ...same idea :)



    So.... send your email without outputting *anything* to the page, not even a white space, then use the header location to redirect to your next page.
     
    mrmonster, Jun 1, 2007 IP
  7. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can always place the header command after sending the email, but you have to understand that you can't send data to the browser then send a location header: it's not 'legal' from an HTTP POV.

    If you want to give feedback to the user you either need to give it at the page where you are redirecting them to, or instead of using header send a meta refresh in the HTML you are sending.
     
    TwistMyArm, Jun 1, 2007 IP
  8. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Technically if you turn on output buffering (ob_start()) then you can have echos before you change the header() because it doesn't actually send out anything until it reaches the end of catches a flush() or other related function. Put your header() after you send the email, and drop an exit; after it.
     
    projectshifter, Jun 1, 2007 IP
  9. rgchris

    rgchris Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    If you wanted to, you could always use an HTML redirect in lieu of the PHP header way.... that won't give you an error when you try to output lines.

    The only problem is that your user might not like being redirected when he's/she's not expecting it.
     
    rgchris, Jun 1, 2007 IP
  10. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Gross. If you're talking about in the header then it's still virtually the same problem, and if you're talking Javascript that's still less efficient since not everyone has javascript enabled (yes, a small percentage of people have it disabled)
     
    projectshifter, Jun 1, 2007 IP
  11. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #11
    If you want it to send the email, then go to thanks.htm, the header() call should go in place of the "message sent" echo statement.
    <?php
    
    $name = $_POST["aname"];
    $fromadd  = $_POST["aemail"];
    $phone  = $_POST["atelephone"];
    $jtitle= $_POST["ajtitle"];
    $company= $_POST["acompany"];
     
    $to = "...@yahoo.com";
    $subject = "Brochure Request";
    $body = "This is a request from the website for a .....,\n\nThe requst has come from ".$name."\n\nTheir phone number is ".$phone."\n\nThey are from the company ".$company."\n\nThier Job Title is ".$jtitle;
    $headers = "From: ".$fromadd."\r\n" .
        "X-Mailer: php";
    if (mail($to, $subject, $body, $headers)) {
        header("Location: http://www..../thanks.htm");
        echo 'Delete this line after testing but tell me if this shows up and page is still not redirecting...';
        exit();
    } else {
        echo("<p>Message delivery failed...</p>");
    }
    
    ?>
    PHP:
     
    krt, Jun 1, 2007 IP