How to redirect users if message is sent or not on a contact form

Discussion in 'PHP' started by Carl29, Nov 18, 2010.

  1. #1
    Hi, I need some help to do the folowing:
    If the message is sent users are redirected to one page, and if for some reason (missing email,...) they are redirected to another.
    Here's the code i have right now: (it works, but it shows a message on the page itself)
    
    <?php
    if(($_POST['email'] == '') || ($_POST['name'] == '') || (is_numeric($_POST['name'])) || ($_POST['surname'] == '') || (is_numeric($_POST['surname'])) || ($_POST['subject'] == '') || ($_POST['message'] == '')) {
    echo 'Complete the forms correctly <br>
          Click <a href="index.php">here</a> to return on main page.'; 
    } else {
    
    $to = 'mail@mail.com'; 
    $send_date = date('d-m-Y H:i:s');
    
    $subject = $_POST['subject'];
    
    $message = '
    <html>
    <head>
    <title>Contact</title>
    </head>
    <body>
    <p><tt>Data trimitere: '.$send_date.' </tt></p>
    <table>
    <tr>
    <td><tt> Name: '.$_POST['name'].' </tt></td>
    </tr>
    <tr>
    <td><tt> Surname: '.$_POST['surname'].' </tt></td>
    </tr>
    <tr>
    <td><tt> E-Mail: <a href="mailto:'.$_POST['email'].'">'.$_POST['email'].'</a> </tt></td>
    </tr>
    <tr>
    <td><tt> Message: <br><br> '.$_POST['message'].' </tt></td>
    </tr>
    </table>
    </body>
    </html>';
    $headere  = "MIME-Version: 1.0\r\n";
    $headere .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headere .= "From: ".$_POST['name']." ".$_POST['surname']."<".$_POST['email'].">\r\n";
    mail($to, $subject, $message, $headere);
    echo 'The message was send';
    }
    ?>
    
    Code (markup):
    Thank you
     
    Carl29, Nov 18, 2010 IP
  2. sponsorlist

    sponsorlist Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you want to automaticly redirect users to another page, try php command like this:
    header( 'Location: http ://www.site.com/page.html');

    But there must be no other output sent to user before this line.
     
    sponsorlist, Nov 18, 2010 IP
  3. Carl29

    Carl29 Active Member

    Messages:
    114
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    I think you missunderstood: I want to redirect them, if the message is sent.
     
    Carl29, Nov 18, 2010 IP
  4. badhim

    badhim Member

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #4
    if (mail($to, $subject, $message, $headere)) {
    h e a d e r('Location: http ://www.site.com/page.html');
    exit;
    }
     
    Last edited: Nov 18, 2010
    badhim, Nov 18, 2010 IP
  5. sponsorlist

    sponsorlist Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    As shown above, you can use return value from mail function to see if it was sent.

    http://php.net/manual/en/function.mail.php
     
    sponsorlist, Nov 18, 2010 IP