chauhan Posted February 1, 2017 Share Posted February 1, 2017 Hi Im trying to force and automatic PDF download after the visitor successfully submits a request form and email goes to company. With the below codes, the email is sent and the submission is successful, but the pdf download does not start. Don't know where am I going wrong. the following is the code: html form <section id="contact" class="pt100 pb90"> <div class="container"> <div class="row"> <div class="col-sm-2"> </div> <div class="col-sm-8"> <h4>Please fill up the below form to download.</h4> </div> <div class="col-sm-2"> </div> </div> <div class="row"> <div class="col-sm-2"> </div> <div class="col-sm-8"> <div id="message"></div> <form method="post" action="php/sendmail.php" name="downloadform" id="downloadform"> <input style="max-width:49%;" name="fname" type="text" id="fname" placeholder="First Name*"/> <input style="max-width:49%;" name="lname" type="text" id="lname" placeholder="Last Name*"/> <input style="max-width:49%;" name="cname" type="text" id="cname" placeholder="Company Name*"/> <input style="max-width:49%;" name="email" type="text" id="email" placeholder="Email*"/> <input style="max-width:49%;" name="phone" type="text" id="phone" placeholder="Phone No*"/> <input style="max-width:49%;" name="mobile" type="text" id="mobile" placeholder="Mobile*"/> <input style="max-width:49%; display:none;" name="product" type="text" id="product" value="Wall Putty FN PDF" disabled/> <input style="max-width:49%; display:none;" name="source" type="text" id="source" value="Bestbuild Website" disabled/> <input style="max-width:49%; display:none;" name="remarks" type="text" id="remarks" value="Wall Putty FN PDF Download" disabled/> <input type="submit" class="submit" id="submit" value="Submit" /> </form> </div> <div class="col-sm-2"> </div> </div> </div> </section> js code $('#downloadform').submit(function(){ var action = $(this).attr('action'); $("#message").slideUp(250,function() { $('#message').hide(); $('#submit') .after('<img src="img/assets/contact-form-loader.gif" class="loader" />') .attr('disabled','disabled'); $.post(action, { source: $('#source').val(), cname: $('#cname').val(), product: $('#product').val(), phone: $('#phone').val(), mobile: $('#mobile').val(), fname: $('#fname').val(), lname: $('#lname').val(), email: $('#email').val(), remarks: $('#remarks').val(), }, function(data){ document.getElementById('message').innerHTML = data; $('#message').slideDown(250); $('#downloadform img.loader').fadeOut('slow',function(){$(this).remove()}); $('#submit').removeAttr('disabled'); if(data.match('success') != null) $('#downloadform').slideUp(850, 'easeInOutExpo'); } ); }); return false; }); php send email code <?php if(!$_POST) exit; function isEmail($email) { return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email)); } if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n"); $source = $_POST['source']; $cname = $_POST['cname']; $product = $_POST['product']; $phone = $_POST['phone']; $mobile = $_POST['mobile']; $fname = $_POST['fname']; $lname = $_POST['lname']; $email = $_POST['email']; $remarks = $_POST['remarks']; $verify = $_POST['verify']; if(trim($fname) == '') { echo '<div class="error_message">You must enter your first name.</div>'; exit(); } else if(trim($lname) == '') { echo '<div class="error_message">You must enter your last name.</div>'; exit(); } else if(trim($cname) == '') { echo '<div class="error_message">You must enter your company name.</div>'; exit(); } else if(trim($phone) == '') { echo '<div class="error_message">You must enter your phone no.</div>'; exit(); } else if(trim($mobile) == '') { echo '<div class="error_message">You must enter your mobile no.</div>'; exit(); } else if(trim($email) == '') { echo '<div class="error_message">Please enter a valid email address.</div>'; exit(); } else if(!isEmail($email)) { echo '<div class="error_message">You have enter an invalid e-mail address, try again.</div>'; exit(); } if(get_magic_quotes_gpc()) { $remarks = stripslashes(remarks); } $address = "example@example.com"; $e_subject = $cname . '/' . $fname . ' ' . $lname . ' ' . $source . ' '; $e_source = "Source: $source" . PHP_EOL . PHP_EOL; $e_cname = "Custome Name: $cname" . PHP_EOL . PHP_EOL; $e_product = "Product: $product" . PHP_EOL . PHP_EOL; $e_phone = "Phone No.: $phone" . PHP_EOL . PHP_EOL; $e_mobile = "Mobile: $mobile" . PHP_EOL . PHP_EOL; $e_name = "Contact Person: $fname $lname" . PHP_EOL . PHP_EOL; $e_email = "Email: $email" . PHP_EOL . PHP_EOL; $e_remarks = "Remarks: $remarks"; $msg = $e_source . $e_cname . $e_product . $e_phone . $e_mobile . $e_name . $e_email . $e_remarks; $headers = "From: $email" . PHP_EOL; $headers .= "Reply-To: $email" . PHP_EOL; $headers .= "MIME-Version: 1.0" . PHP_EOL; $headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL; $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL; if(mail($address, $e_subject, $msg, $headers)) { echo "<fieldset>"; echo "<div id='success_page'>"; echo "<h4 class='highlight'>Thank you! <strong>$fname</strong>. PDF download will automatically start right now.</h4>"; echo "</div>"; echo "</fieldset>"; header( "Refresh:5; url=download.php"); } else { echo 'ERROR!'; } download php code <?php header("Content-type:application/pdf"); header("Content-Disposition:attachment;filename=sample.pdf"); readfile("sample.pdf"); ?> Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 1, 2017 Share Posted February 1, 2017 You cannot set a header when you've already generated output. The HTTP headers come before the body, so once you start rendering the page content, it's too late to do anything with the headers. This means you must organize your script properly and separate the programming logic from the output. In your case, I suggest you forget about the output and just send the document. Making the user wait 5 seconds and messing with page refreshes is annoying and antiquated. Your mail code is also badly broken, but that's another story. Quote Link to comment Share on other sites More sharing options...
chauhan Posted February 1, 2017 Author Share Posted February 1, 2017 This is my first project in php. Can you help me correct the code. I tried using the echo statements but the download still did not happen Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 1, 2017 Share Posted February 1, 2017 Read my reply. Quote Link to comment Share on other sites More sharing options...
chauhan Posted February 1, 2017 Author Share Posted February 1, 2017 I changed the code to the below but still dint work. <?php if(!$_POST) exit; function isEmail($email) { return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email)); } if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n"); $source = $_POST['source']; $cname = $_POST['cname']; $product = $_POST['product']; $phone = $_POST['phone']; $mobile = $_POST['mobile']; $fname = $_POST['fname']; $lname = $_POST['lname']; $email = $_POST['email']; $remarks = $_POST['remarks']; $verify = $_POST['verify']; $address = "abc@gmail.com"; $e_subject = $cname . '/' . $fname . ' ' . $lname . ' ' . $source . ' '; $e_source = "Source: $source" . PHP_EOL . PHP_EOL; $e_cname = "Custome Name: $cname" . PHP_EOL . PHP_EOL; $e_product = "Product: $product" . PHP_EOL . PHP_EOL; $e_phone = "Phone No.: $phone" . PHP_EOL . PHP_EOL; $e_mobile = "Mobile: $mobile" . PHP_EOL . PHP_EOL; $e_name = "Contact Person: $fname $lname" . PHP_EOL . PHP_EOL; $e_email = "Email: $email" . PHP_EOL . PHP_EOL; $e_remarks = "Remarks: $remarks"; $msg = $e_source . $e_cname . $e_product . $e_phone . $e_mobile . $e_name . $e_email . $e_remarks; $headers = "From: $email" . PHP_EOL; $headers .= "Reply-To: $email" . PHP_EOL; $headers .= "MIME-Version: 1.0" . PHP_EOL; $headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL; $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL; if(mail($address, $e_subject, $msg, $headers)) { header( "Refresh:5; url=download.php"); } else { echo 'ERROR!'; } 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.