@GlubokovD

Как сделать переадресацию после выполнения PHP формы?

Имею код формы отправки - нужно реализовать переход на страницу "Спасибо" после срабатывания формы
Подскажите пожалуйста
Как реализовать это на PHP?
Добавлял header("Location: полный путь"); ... перед print_r($var);
Но не работает.
Заранее спасибо.

<?php
$method = $_SERVER['REQUEST_METHOD'];
//Script Foreach
$c = true;
if ( $method === 'POST' ) {
    $project_name = trim($_POST["project_name"]);
    $admin_email  = trim($_POST["admin_email"]);
    $form_subject = trim($_POST["form_subject"]);
    foreach ( $_POST as $key => $value ) {
        if ( $value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject" ) {
            $message .= "
			" . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
				<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
				<td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
			</tr>
			";
        }
    }
} else if ( $method === 'GET' ) {
    $project_name = trim($_GET["project_name"]);
    $admin_email  = trim($_GET["admin_email"]);
    $form_subject = trim($_GET["form_subject"]);
    foreach ( $_GET as $key => $value ) {
        if ( $value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject" ) {
            $message .= "
			" . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
				<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
				<td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
			</tr>
			";
        }
    }
}
$message = "<table style='width: 100%;'>$message</table>";
function adopt($text) {
    return '=?UTF-8?B?'.Base64_encode($text).'?=';
}
$headers = "MIME-Version: 1.0" . PHP_EOL .
    "Content-Type: text/html; charset=utf-8" . PHP_EOL .
    'From: '.adopt($project_name).' <'.$admin_email.'>' . PHP_EOL .
    'Reply-To: '.$admin_email.'' . PHP_EOL;
mail($admin_email, adopt($form_subject), $message, $headers );




abstract class Gateway
{
    public $name;

    protected $host;
    protected $port;
    protected $apiUrl;

    protected $proto_part;
    protected $port_part;

    protected $handle;
    protected $botToken;

    public function __construct($token, $options = [])
    {
        if ($this->name == null) {
            throw new Exception('Gateway name is empty!');
        }

        $this->handle = curl_init();
        $this->host = $host = $options['host'];
        $this->port = $port = $options['port'];
        $this->botToken = $token;

        $this->proto_part = ($port == 443 ? 'https' : 'http');
        $this->port_part = ($port == 443 || $port == 80) ? '' : ':' . $port;
    }

    abstract public function request($method, $params = []);
}

class TelegramGateway extends Gateway
{
    public function __construct($token, $options = [])
    {
        $this->name = 'telegram';

        $options += [
            'host' => 'api.telegram.org',
            'port' => 443,
        ];

        parent::__construct($token, $options);

        $this->apiUrl = "{$this->proto_part}://{$this->host}{$this->port_part}/bot{$token}";
    }

    public function request($method, $params = [])
    {
        $url = $this->apiUrl . '/' . $method;
        $query = http_build_query($params);

        curl_setopt($this->handle, CURLOPT_URL, $url);
        curl_setopt($this->handle, CURLOPT_POST, true);
        curl_setopt($this->handle, CURLOPT_POSTFIELDS, $query);
        curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, true);

        $response_str = curl_exec($this->handle);
        $response = json_decode($response_str, true);

        return $response;
    }
}

$gateway = new TelegramGateway('*********************');
$message = json_encode($_POST, JSON_UNESCAPED_UNICODE);
$var = $gateway->request('sendMessage', [
    'chat_id'    => '****',
    'text'       => $message,
    'parse_mode' => 'HTML',
]);
print_r($var);
die(success);


Либо же JS Часть
$('form').each(function(index, el) {
		$(el).validate({
			rules:{
				"phone":{ required:true }
			},
			submitHandler: function(form){
				$(form).ajaxSubmit({
					type: 'POST',
					url: 'mail.php',
					success: function() {
						testSlider.goToSlide( $('.step-slide').length - 1 );
						$('.header-line').slideUp(300);
						$('.progress-line').slideUp(300);
					}
				});
			}
		});
	});
  • Вопрос задан
  • 126 просмотров
Решения вопроса 1
@magarif
Программист
функция header может не работать, если был вывод перед ней.

Тут есть несколько вариантов:
- Перед открывающим тегом <?php нет никаких символов?
- Может кодировка UTF-8 с BOM?
- Может этот файл подключается в другом файле, где уже есть какой-то вывод?
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

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

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