Web Design Forum: Form Hell! - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Form Hell!

#1 User is offline   Kallak 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 14-October 11
  • Reputation: 0

Posted 01 February 2012 - 03:18 AM

Hi everyone,

I'm trying desperately to have this form operate the way I want it to.

Basically, when the user submits this form, I want a download box to open for the PDF target file, rather than the PDF loading in the browser.

Here is the code:

<FORM METHOD="POST" ACTION="../cgi-bin/sender.pl" name="Free Report">

<input type=hidden name="redirect" value="../Report.pdf" />

<input type="submit" id="button" value="Download Now!" onclick="MM_validateForm('name','','R','email','','RisEmail');return document.MM_returnValue" />


Any help would be much appreciated, thank you!

This post has been edited by Renaissance-Design: 02 February 2012 - 04:23 PM
Reason for edit: Please use the code button or tags to format your code.

0

#2 User is offline   FizixRichard 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 325
  • Joined: 05-October 07
  • Reputation: 47
  • Gender:Male
  • Location:Market Deeping, England
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 01 February 2012 - 01:35 PM

OK, you don't want the form to be linking directly to the PDF as the browsers default behavior (to load it in the browser) will take precedence. I assume thats whats happening (the pdf name field is redirect so I suspect your script redirects to the file?)


Instead you want the form to lead to a script that will cause the file (the PDF) to download, basically by setting a header type and loading the file that way.

I'll give you some code but it is untested and is just about as simple as it gets (but there again it doesn't need to be particularly complex.


So your form tag will look something like this:

<form action="download.php" method="post">



Then you will have a PHP script to download the file in question:

<?php

   // Set the details of the file to load, this may be hard coded or dynamic (i.e. form has a fileid field 
   // that this script can use to lookup the file in the database.

   $file['filename'] = "somefile.pdf";

   // The path to the file
   $filepath = "files/" . $file['filename'];
	
	
   // Set the content-type
   header('Content-type: text/plain'); // This will be the type for the file, so you'll want to change this
	
   // Will cause the script to show the Open / Save dialog
   header("Content-Disposition: attachment; filename=\"$filepath\"");

   // Read the file from the server
   readfile("$filepath");

?>




Or you can do it like this:


<?php

// -------------------------------------------------------------------------------------------------------
// Do file download
// -------------------------------------------------------------------------------------------------------
$fileid = $_POST['fileid'];		// Clean your input here using whatever mechanism you are using

	
// Validate those details and fetch the files details from the DB here, or just set them
// The files details will go into $download
/*
...

The $download array should contain at a minimum
$download['filename'] // The name of the file (i.e. 'myPDFFile', no dot or extension)
$download['format'] // The format of the file (pdf, no '.')
$download['mimetype'] // The mimetype of the file (PDF is 'application/pdf')

...
*/
	
	
	
// OK, set the files download path (tidy this up)
$filepath = "files/";

$fullpath = $filepath . $download['filename'] . "." . $download['format'];

$filename = $download['filename'] . "." . $download['format'];
	

// Fetch the file	
if ($fd = fopen ($fullpath, "r")) 
{
    $fsize = filesize($fullpath);	// Get the filesize
    $ext = $download['format']; 	// Set the format into $ext
			 
    switch ($ext) 
	{
		case "pdf":
           	header("Content-type: application/pdf"); // add here more headers for different extensions
           	header("Content-Disposition: attachment; filename=\"".$filename."\""); // use 'attachment' to force a download
          	break;
        default;
           	header("Content-type: application/octet-stream");
            header("Content-Disposition: filename=\"".$filename."\"");
	}
               
	header("Content-length: $fsize");
	header("Cache-control: private"); //use this to open files directly
			 
	while(!feof($fd)) 
	{
         $buffer = fread($fd, 2048);
         echo $buffer;
	}
}
	
fclose ($fd);
exit;


?>





I notice your script, in the form header, is a perl script. I haven't used perl since Ultimate Bulletin Board was king of the communities (I used to love hacking that forum, shame its gone now... ah the nostalgia)... Shame they screwed it all up.

anyway...

I cannot remember the code for causing a file to download in perl, but if I remember correctly its something pretty similar to the second example.

The first I just showed for demonstrative purposes, use the second to power it if you use PHP. Otherwise google "file download in perl" and it should come up with the answer.

This post has been edited by FizixRichard: 01 February 2012 - 01:49 PM

0

#3 User is offline   Kallak 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 14-October 11
  • Reputation: 0

Posted 02 February 2012 - 04:01 PM

Ah, thank you so much! Will try this right away :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users