Submitting form automatically with php

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Vagam
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Feb 14, 2018 11:34 am

Wed Feb 14, 2018 11:40 am

I need to submit a form automatically, so I have the following code:

Code: Select all

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=\"hidden\" name=\"submit\" value=\"Download\">";
	   
	   echo "</form>";  
	   	   
	 echo "<script language = \"JavaScript\"> document.myform.submit()\;</script>"; 
This is how I have learned to do it from the internet, but it doesn't work. Can anyone help?
Thanks in advance.

chorn
php-forum Fan User
php-forum Fan User
Posts: 564
Joined: Fri Apr 01, 2016 2:18 am

Thu Feb 15, 2018 12:23 am

Code: Select all

I need to submit a form automatically
What for do you want to send an empty form to the same script? All you get is an additional POST request, you already have all data on the server side.

Vagam
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Feb 14, 2018 11:34 am

Thu Feb 15, 2018 2:46 am

Because I want to download a program. At the top of the script I have the following:

Code: Select all

if ($_SERVER["REQUEST_METHOD"] == "POST") {
     
   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();
   }
I could also have this in a separate script. But it works like this too.

chorn
php-forum Fan User
php-forum Fan User
Posts: 564
Joined: Fri Apr 01, 2016 2:18 am

Thu Feb 15, 2018 3:31 am

just put the download code on top of where you want to output the form. or use a form for the initial user action.

Vagam
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Feb 14, 2018 11:34 am

Thu Feb 15, 2018 4:10 am

But then I have to start another script, so how do I do that?

chorn
php-forum Fan User
php-forum Fan User
Posts: 564
Joined: Fri Apr 01, 2016 2:18 am

Thu Feb 15, 2018 4:11 am

what other script? you have a user action, put the code there (move the whole thing to a function)

Vagam
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Feb 14, 2018 11:34 am

Thu Feb 15, 2018 4:36 am

It doesn't work. The headers have to come at the beginning of a script.

chorn
php-forum Fan User
php-forum Fan User
Posts: 564
Joined: Fri Apr 01, 2016 2:18 am

Thu Feb 15, 2018 7:17 am

yes, that's right. you should follow the IPO model.

Vagam
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Feb 14, 2018 11:34 am

Thu Feb 15, 2018 8:26 am

Could you elaborate on that?

chorn
php-forum Fan User
php-forum Fan User
Posts: 564
Joined: Fri Apr 01, 2016 2:18 am

Thu Feb 15, 2018 8:32 am

what ever user action you are expectiong, it has to be followed by the same procedure to download your file. If something has to be outputted, that's some other link or form.

Vagam
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Feb 14, 2018 11:34 am

Thu Feb 15, 2018 10:18 am

Ok. So how shall I do this in my program to make it work?

Vagam
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Feb 14, 2018 11:34 am

Sun Feb 18, 2018 12:40 pm

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

Post Reply