0

I am making a splash page where a user has to enter in their name and email address to download a book. This is my first time doing this. When the user fills out the form and hits submit, I want the book to start downloading automatically and I want their form details to get sent to my email so I can add them to a mailing list.

Right now my HTML form looks like this (I am using formmail):

<form id="myform" method="post" action="submit.php" name="myform" width="100%">
<input type="hidden" name="good_url" value="index.html#submitgood" />
<input type="hidden" name="bad_url" value="index.html#submitbad" />
<input type="hidden" name="env_report" value="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT,AUTH_TYPE,REMOTE_USER" />
<input type="hidden" name="derive_fields" value="email=EmailAddr,realname=username" />
<input type="hidden" name="recipients" value="myaddress" />
<input type="hidden" name="subject" value="Book Download" />
<fieldset><legend>Please fill out the information below to download.<br><br>Filesize, 59.8 MB.</legend>
<table><tr><td><label for="Name" id="namelabel"><strong>Full name:</strong></label><br>
<input id="Name" type="text" name="username" title="Enter your full name" placeholder="Your Name" autofocus required /></td></tr>
<tr><td><label for="eMail" id="emaillabel"><strong>Email address:</strong></label><br>
<input id="eMail" type="email" name="EmailAddr" title="Enter your email address" placeholder="[email protected]" required /></td></tr>
<tr><td><label for"eMail_repeat" id="emaillabel2"><strong>Repeat Email address:</strong></label>
<input id="eMail_repeat" type="email" name="email_addr_repeat" title="Repeat your email address" placeholder="[email protected]" required oninput="check(this)" /></td></tr>
</table><br>
<input id="reset" type="reset" name="reset" value="Clear" />
<input id="submit" type="submit" name="submit" value="Submit" />
<input type="hidden" name="mail_options" value="HTMLTemplate=MYURL/fmtemplates/mailtemplate.html" /></fieldset></form>

You can see, when the user hits submit it accesses submit.php, which looks like this:

<?php
require_once('formmail.php');
require_once('pocketdownload.php');
?>

So, the issue is, the first object, (in this case, formmail.php) works perfectly and I get the email with their information, but the download (which is what pocketdownload.php does) never happens. When I switch them around (put pocketdownload.php before formmail.php) the download happens perfectly, but the email never sends and the success popup telling the user Thank You for downloading, never appears. Basically, with this PHP code, only the first one will work.

This is what pocketdownload.php looks like:

<?php
$file_url = 'http://LOCATION_OF_MY_FILE';
header("Content-type: application/pdf");
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
readfile($file_url);
?>

SO! Is there any way that when the user hits SUBMIT that BOTH of those .php scripts will run at the same time? Am I making a mistake in my PHP code? I heard that this can be done in Ajax, but I have no clue how to implement something like that. Can anyone show me the way? Thanks!!!


EDIT: I solved my own problem by leaving submit.php the way it is and changing the popup that appears when the user hits submit. It used to say "Thank you for downloading", but I changed the popup to say "Click HERE to download" (with HERE being a hyperlink to the file).

It's not flashy, it's not pretty - but it got the job done.

6
  • 3
    No. a form submits to a SINGLE action only. But you can use javascript to do multiple submissions, e.g. ajax calls, clone forms and silently submit clones, blah blah blah. But in pure html, a form submits to one place and one place only.
    – Marc B
    Oct 23, 2013 at 19:22
  • What does formmail.php do that prevents the download?
    – Matt Ball
    Oct 23, 2013 at 19:23
  • 1
    Of course your script can do both things. There's no reason why one script can't generate an email, and then go on to produce a download. That said, it won't necessarily work with the scripts that you have since one (probably formmail.php) might do something that interferes. You haven't posted formmail.php so I can't say for sure.
    – user1864610
    Oct 23, 2013 at 19:26
  • 1
    @MarcB I think the OP understands the 'single action' idea, but he's attempting to combine multiple actions in one script. This should work in principle, but possibly not with the scripts he has.
    – user1864610
    Oct 23, 2013 at 19:28
  • To @MikeW, formmail.php is available for free online and is 20,000 lines. I can't include that in here.
    – warnakey
    Oct 24, 2013 at 14:15

1 Answer 1

-2

if the submit is succesfull just redirect to the pdf.

example

<?php

if(isset($_POST['submit'])) {

  /* do your things */

  header("Location: http://mywebsite.com/book.pdf")
  # OR
  echo '<meta http-equiv="Refresh" content="0; url=http://mywebsite.com/book.pdf" />';

}

?>
3
  • I tried changing submit.php to <?php if(isset($_POST['submit'])) { require_once('formmail.php'); require_once('pocketdownload.php'); echo '<meta http-equiv="Refresh" content="0; url=MYURL.pdf" />'; } ?> Didn't work =[
    – warnakey
    Oct 23, 2013 at 19:56
  • Neither approach will necessarily start a download. The most likely outcome is that the browser will just open the PDF file.
    – user1864610
    Oct 23, 2013 at 19:56
  • I would love it if the browser would open the PDF file, because then they could just save it from there, but the "header" line of code doesn't work at all. (Gave me an error) and the "echo" line of code has the same result as my original code. It doesn't open the pdf or download it, it just sends me an email with the information filled out in the form.
    – warnakey
    Oct 23, 2013 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.