@proclub

Как сделать редирект после заполнения формы?

Вот форма, она подвязана к javascript файлу и отправка идет с ajax, но вопрос, можно как-то сделать редирект сразу после отправки формы на другой сайт ?

<?php
/*
 *  CONFIGURE EVERYTHING HERE
 */

// an email address that will be in the From field of the email.
$from = 'Тестовая форма <test1@mail.ru>';

// an email address that will receive the email with the output of the form
$sendTo = 'Тестовая форма <test1@mail.ru>';

// subject of the email
$subject = 'Тест с формы на сайте';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); 

// message that will be displayed when everything is OK :)
$okMessage = 'Ваша заявка успешно отправлена!';

// If something goes wrong, we will display this message.
$errorMessage = 'Произошла ошибка при отправке формы. Пожалуйста, попробуйте еще раз или воспользуйтесь контактными данными на сайте';

/*
 *  LET'S DO THE SENDING
 */

// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

try
{

    if(count($_POST) == 0) throw new \Exception('Form is empty');
            
    $emailText = "You have a new message from your contact form\n=============================\n";

    foreach ($_POST as $key => $value) {
        // If the field exists in the $fields array, include it in the email 
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    // All the neccessary headers for the email.
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );
    
    // Send email
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}


// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
// else just display the message
else {
    echo $responseArray['message'];
}
  • Вопрос задан
  • 157 просмотров
Решения вопроса 1
gscraft
@gscraft
Программист, философ
Редирект делает браузер, Вам нужно отправить заголовок Location: header("Location: /{$url}", true, $status); — однако, стоит учитывать, что это может не сработать с некоторыми ajax-библиотеками, там уж смотрите по обстоятельствам.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы