I'm new to PHP and I'm trying to create a form which when the user fills out, is redirected to my success page and then a download is initiated and their details are saved to a .txt file.
I've managed to do all of the above but the user isn't redirected, but the code to download the file still runs (which is located in the success page).
Why is this happening?
If I removed the code to download the file then the user is redirected, do I need to add a delay before the file download?
Index.php
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form action="myform.php" method="post">
Name <input type="text" name="name" value="">
Company <input type="text" name="company" value="">
Website <input type="text" name="website" value="">
Email <input type="text" name="email" value="">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
myform.php
<?php
$name = @$_POST['name'];
$company = @$_POST['company'];
$website = @$_POST['website'];
$email = @$_POST['email'];
$filename = "info.txt";
$f_data= '
Name : '.$name.'
Email : '.$email.'
Website: '.$website.'
Company: '.$company.'
============================================================================= =
';
$file = fopen($filename, "a");
fwrite($file,$f_data);
fclose($file);
header("Location:success.php");
?>
success.php
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
header("Content-Disposition: attachment; filename=guide.pdf");
header("Content-Type: application/octet-stream");
header("Content-Length: ".filesize("guide.pdf"));
header("Pragma: no-cache");
header("Expires: 0");
$fp = fopen("guide.pdf", "r");
print fread($fp, filesize("guide.pdf"));
fclose($fp);
exit();
?>
<html>
<h1>Congrats</h1>
</html>
Success!
at the end of the code instead, still the same result :/ – Joe D Sep 27 '15 at 15:23