0

Hi,

I am trying to get a form to redirect to a thank you page but after several attempts I am still no closer to achieving this. Below is the php code form the sendemail script:

<?php

    $name = trim($_POST['name']);
    $email = $_POST['email'];
    $comments = $_POST['comments'];

    $site_owners_email = '**************'; // Replace this with your own email address
    $site_owners_name = 'sales'; // replace with your name

    if (strlen($name) < 2) {
        $error['name'] = "Please enter your name";  
    }

    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Please enter a valid email address"; 
    }

    if (strlen($comments) < 3) {
        $error['comments'] = "Please leave a comment.";
    }

    if (!$error) {

        require_once('phpMailer/class.phpmailer.php');
        $mail = new PHPMailer();

        $mail->From = $email;
        $mail->FromName = $name;
        $mail->Subject = "Website Contact Form";
        $mail->AddAddress($site_owners_email, $site_owners_name);
        $mail->Body = $comments;

        // If you want to use receive your email at Gmail then uncomment the below lines and enter your gmail address and password.

        $mail->Mailer = "smtp";
        $mail->Host = "*********"; 

        $mail->SMTPAuth = true; // turn on SMTP authentication
        $mail->Username = "***********"; // SMTP username
        $mail->Password = "********"; // SMTP password

        $mail->Send();

        echo "<li class='success'>Thanks " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>";

    } # end if no error
    else {

        $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
        $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
        $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null;

        echo $response;
    } # end if there was an error sending

?>

I have tried replacing "echo "<li class='success'>Thanks " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>";" with a header that redirects to a new location but this just doesnt work.

Any help would be appreciated.

Thanks

3
Contributors
11
Replies
58
Views
3 Years
Discussion Span
Last Post by ehpratah
0

What is the error when you say 'it just doesn\'t work'?
You described way of doing that.
Btw. you could try if mailer class is working with one if condition (ln. 42):

<?php

    $name = trim($_POST['name']);
    $email = $_POST['email'];
    $comments = $_POST['comments'];

    $site_owners_email = '**************'; // Replace this with your own email address
    $site_owners_name = 'sales'; // replace with your name

    if (strlen($name) < 2) {
        $error['name'] = "Please enter your name";  
    }

    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Please enter a valid email address"; 
    }

    if (strlen($comments) < 3) {
        $error['comments'] = "Please leave a comment.";
    }

    if (!$error) {

        require_once('phpMailer/class.phpmailer.php');
        $mail = new PHPMailer();

        $mail->From = $email;
        $mail->FromName = $name;
        $mail->Subject = "Website Contact Form";
        $mail->AddAddress($site_owners_email, $site_owners_name);
        $mail->Body = $comments;

        // If you want to use receive your email at Gmail then uncomment the below lines and enter your gmail address and password.

        $mail->Mailer = "smtp";
        $mail->Host = "*********"; 

        $mail->SMTPAuth = true; // turn on SMTP authentication
        $mail->Username = "***********"; // SMTP username
        $mail->Password = "********"; // SMTP password

//      $mail->Send();

//      echo "<li class='success'>Thanks " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>";

        if ($mail->Send())
        {
            header('Location: http://www.google.com/');
        }

    } # end if no error
    else {

        $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
        $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
        $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null;

        echo $response;
    } # end if there was an error sending

?>
0

Hi,

There is no error, it just doesnt redirect after form submission.

0

Thanks for your attempt but I still do not get a redirect, the form sends perfectly but it does not redirect to a new page afterwards.

0

So something is very wrong there. It is inbuilt PHP function and if that doesn't work, contact page and sending mail should be your most irrelevant problem.

Google why your header() function doesn't work.

0

Thanks for the help,

This is something that was done before I took a look at it, perhaps best option is to just build a new form.

Thanks

0

Hi have you tried using js for redirection? just for debugging purpose try

window.location.href="thankyou.php";

see if its working

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.