Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Building Websites with PHP Contact Form & Sending Email Sending Our Email

Qasim Hafeez
Qasim Hafeez
12,731 Points

Redirect issue

When I submit the form, the redirect takes me to my local host file. Here is my code:

$app->post('/contact', function() use($app){
    $name = $app->request->post('name');
    $email = $app->request->post('email');
    $msg = $app->request->post('msg');

    if(!empty($name) && !empty($email) && !empty($msg)){
        $cleanName = filter_var($name, FILTER_SANITIZE_STRING);
        $cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
        $cleanMsg = filter_var($msg, FILTER_SANITIZE_STRING);
    } else {

        //message the user that there was a problem
        $app->redirect('contact');
    }

    $transport = Swift_SendmailTransport::newInstance('/user/sbin/sendmail -t');
    $mailer = \Swift_Mailer::newInstance($transport);

    $message = \Swift_Message::newInstance();
    $message->setSubject('Email From Our Website');
    $message->setFrom(array(
        $cleanEmail => $cleanName
    ));

    $message->setTo(array('qhafeez26@gmail.com')

        );
    $message->setBody($cleanMsg);

    $result = $mailer->send($message);

    if($result > 0){
        //send a message that says thank you
        $app->redirect('/');
    } else {
        //send a message to the user that the mesage failed to send
        //log that there was an error
        $app->redirect('contact');
    }


});

I've tried changing the "/" in the if-else block to the root of the website but it creates a different problem. Instead of going to 'localhost/simple_php_website' it goes to 'localhost/simple_php_website/simple_php_website'