Osram Posted February 16, 2018 Share Posted February 16, 2018 I have the following empty form that needs to be submitted automatically, but which doesn't work: echo "<form name = \"myform\" id = \"myform\" style= \"margin-left:180px; padding-top:100px\" method=\"post\" action=\""; echo htmlspecialchars($_SERVER["PHP_SELF"]); echo "\">"; echo "<input type=\"hidden\" name=\"Next\" value =\"1\">"; echo "<input style = \"margin-left:260px\" type=\"submit\" name=\"submit\" value=\"Download\">"; echo "</form>"; echo "<script>document.getElementById('myform').submit();</script>"; The reason I have this form is that I have a downloading routine at the beginning of the script: if ($_SERVER["REQUEST_METHOD"] == "POST") { $OK=1; if($_POST["Next"]==1){ $exe = "setup.exe"; header("Content-Type: application/octet-stream"); // binary file header("Content-Disposition: attachment; filename=\"setup.exe\""); // download as "Installation-file.exe" header("Content-Length: " . filesize($exe)); // tells the browser and user how large the file download will be readfile($exe); // dump the file exit(); } Can anyone help? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 16, 2018 Share Posted February 16, 2018 For downloading, the form and download code neeed to be in separate scripts. Set form action to the download script. Quote Link to comment Share on other sites More sharing options...
Osram Posted February 16, 2018 Author Share Posted February 16, 2018 Well, the thing is that the script works as it is now by clicking the submit button. The download works fine. The only thing is that I want to avoid having to click the submit button, so that the submit happens automatically. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 16, 2018 Share Posted February 16, 2018 (edited) Is there a reason for having the form? I imagine there's something directing the user to download the installation file, maybe a link. Could that link just go directly to the download script and bypass the form altogether? Edited February 16, 2018 by cyberRobot fixed typo Quote Link to comment Share on other sites More sharing options...
Osram Posted February 16, 2018 Author Share Posted February 16, 2018 Yes, as long as the process is automatic, it doesn't matter which way it is done. I have also tried different commands to make the script start another script, but even that I have not been able to do. Quote Link to comment Share on other sites More sharing options...
Osram Posted February 18, 2018 Author Share Posted February 18, 2018 So I finally found the solution myself. I put the download script in a separate file and used the following command: window.open ('dl.php','_self',false); Thanks for trying to help. Sincerely Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.