Получился вот такой рабочий макет:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<form action="email.php" method="POST" id="form1">
<input type="text" name="file" value='true' hidden>
<input type="text" name="name" placeholder="Ваше имя">
<input type="phone" name="phone" placeholder="Ваш телефон">
<input type="email" name="email" placeholder="Ваша почта">
<button class="buttonIntroduction">Получить консультацию</button>
</form>
<form action="email.php" method="POST" id="form2">
<input type="text" name="name" placeholder="Ваше имя">
<input type="phone" name="phone" placeholder="Ваш телефон">
<input type="email" name="email" placeholder="Ваша почта">
<button class="buttonIntroduction">Получить консультацию</button>
</form>
<div id="buffer"></div>
<script>
$("form").submit(function(event){
var a = new FormData(this);
if (!a.get('file')) event.preventDefault();
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: a,
contentType: false,
processData: false,
cache: false,
success: function(resp){
if(resp == 'noFile') $('#buffer').html("Всем спасибо");
else $('#buffer').html("Смотрите файл");
}
});
});
</script>
<?php
$file = 'CatlView772_8417.pdf';
if($_POST['file']) {
if (file_exists($file) ) {
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
}
echo('noFile');
?>
При отправке первой формы происходит загрузка файла с сервера на клиент, при отправке второй - нет.
Перезагрузки страницы нет в обоих случаях.