Я наконец нашел решение!Как вы указали мне, Оливер, AJAX был не лучшим способом определить заголовок, как я хотел.Следовательно, когда выполняется событие onclick, я перенаправляю на страницу обработки php, чтобы перейти к загрузке zip-файла!
<?php
require('config/config.php');
require('fpdf/fpdf.php');
if(!isset($_SESSION))
{
session_start();
}
$output = "";
if(isset($_SESSION['user']))
{
$currentTime = time();
if($currentTime < $_SESSION['expire'])
{
if(!empty($_GET['candidacy']))
{
$stmt = $cnx->prepare('SELECT * FROM candidacies WHERE id = :candidacy');
$stmt->bindValue(':candidacy', $_GET['candidacy'], PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $key => $value)
{
switch($value['zipcode'])
{
case "North":
$zipcode = "Nord";
break;
case "South":
$zipcode = "Sud";
break;
case "East":
$zipcode = "Est";
break;
case "West":
$zipcode = "Ouest";
break;
}
switch($value['transportMeans'])
{
case "onFoot":
$transportMeans = "À pied";
break;
case "vehicle":
$transportMeans = "Voiture";
break;
case "bike":
$transportMeans = "Moto";
break;
case "bus":
$transportMeans = "Bus";
break;
case "carpool":
$transportMeans = "Covoiturage";
break;
case "bicycle":
$transportMeans = "Vélo";
break;
}
switch($value['candidacyType'])
{
case "mysteryCustomer":
$candidacyType = "Client Mystère";
break;
case "investigationUnit":
$candidacyType = "Chargé d'enquêtes";
break;
case "investigators":
$candidacyType = "Enquêteurs/Enquêtrices";
break;
}
$nameUser = $value['lastName']." ".$value['firstName'];
$age = $value['age'];
$employment = $value['employment'];
$zipcode = $zipcode;
$transportMeans = $transportMeans;
$scheduleRange = $value['scheduleRange'];
$emailUser = $value['email'];
$phoneNumber = $value['phoneNumber'];
$candidacyType = $candidacyType;
$coveringLetter = $value['coveringLetter'];
$curriculumVitae = $value['curriculumVitae'];
$createdAt = $value['createdAt'];
}
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Image('img/candidacy.jpg',10,6,25);
$pdf->SetFont('Times','',13);
$pdf->Cell(60);
$pdf->Cell(80,10, utf8_decode('Candidature'),1,1,'C');
$pdf->ln();
$pdf->Cell(60);
$pdf->Cell(80,10, $createdAt,1,1,'C');
$pdf->ln();
$pdf->Cell(50,10, 'Nom complet : ', 0,0);
$pdf->Cell(65,10, utf8_decode($nameUser), 0,1);
$pdf->Cell(50,10, utf8_decode('Âge : '),0,0);
$pdf->Cell(65,10, utf8_decode($age), 0,1);
$pdf->Cell(50,10,'Profession : ',0,0);
$pdf->Cell(65,10, utf8_decode($employment), 0,1);
$pdf->Cell(50,10, 'Secteur : ',0,0);
$pdf->Cell(65,10, utf8_decode($zipcode), 0,1);
$pdf->Cell(50,10, 'Moyen de transport :',0,0);
$pdf->Cell(65,10, utf8_decode($transportMeans), 0,1);
$pdf->Cell(50,10, 'Horaires : ',0,0);
$pdf->Cell(65,10, utf8_decode($scheduleRange), 0,1);
$pdf->Cell(50,10, 'Email : ', 0,0);
$pdf->Cell(100,10, utf8_decode($emailUser), 0,1);
$pdf->Cell(50,10, utf8_decode('N° de téléphone : '),0,0);
$pdf->Cell(65,10, utf8_decode($phoneNumber), 0,1);
$pdf->Cell(50,10, 'Candidature : ',0,0);
$pdf->Cell(65,10, utf8_decode($candidacyType), 0,1);
$file = 'Candidature-'.$nameUser.'.pdf';
$pdf->Output('F', $file);
$files = array($file, "candidatures/".$coveringLetter, "candidatures/".$curriculumVitae);
if(extension_loaded('zip'))
{
$zip = new ZipArchive();
$zip_name = md5(random_bytes(64)).".zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
$error .= "Création impossible du zip !";
}
foreach($files as $content)
{
$zip->addFile($content);
}
$zip->close();
if(file_exists($zip_name))
{
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name);
header('Content-Length: ' . filesize($zip_name));
readfile($zip_name);
unlink($zip_name);
}
}
else
{
$error .= "Vérifier l'extension !";
}
$query = $cnx->prepare('UPDATE candidacies SET lastExport = now() WHERE id = :candidacy');
$query->bindValue(':candidacy', $_GET['candidacy'], PDO::PARAM_INT);
$query->execute();
}
}
}
else
{
unset($_SESSION['user']);
session_destroy();
header('Location: ../login.php');
exit(0);
}
function zipExtract(candidacy)
{
window.location.href = ("../zipCandidacy.php?candidacy="+candidacy);
}