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

Recommended Answers

All 11 Replies

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

?>

Hi,

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

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.

Bump! Can anyone else help with this issue?

Thanks but still same result :(

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.

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

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

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

see if its working

where would i insert this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.