I want to allow visitors to download a PDF after they click the form submit button.
The PDF should automatically initiate the "save as dialog" after the Download button is pressed and form details sent.
I am using Javascript to send the details to a PHP form processor. I'm not an expert in either languages and I put the code together from various examples I found online.
In a nutshell, I dont know how to initiate the download automatically after form submit and I dont know whether I should use PHP or Javascript to achieve it.
Appreciate in advance any help or advise given. Thanks
This is the Javascript
$("#fhb_download_form").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
formError1();
submitMSG1(false, "Did you fill in the form properly?");
} else {
event.preventDefault();
submitForm1();
}
});
function submitForm1(){
var fhb_fullname = $("#fhb_fullname").val();
var fhb_email = $("#fhb_email").val();
$.ajax({
type: "POST",
url: "php/fhb_form.php",
data: "fhb_fullname=" + fhb_fullname + "&fhb_email=" + fhb_email,
success : function(text){
if (text == "success"){
formSuccess1();
} else {
formError1();
submitMSG1(false,text);
}
}
});
}
function formSuccess1(){
$("#fhb_download_form")[0].reset();
submitMSG1(true, "Download Ebook")
}
function formError1(){
$("#fhb_download_form").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
$(this).removeClass();
});
}
function submitMSG1(valid, msg){
if(valid){
var msgClasses = "h3 text-center tada animated text-success";
} else {
var msgClasses = "h3 text-center text-danger";
}
$("#fhb_msgSubmit").removeClass().addClass(msgClasses).text(msg);
}
And this is the PHP processing code
$errorMSG = "";
if (empty($_POST["fhb_fullname"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["fhb_fullname"];
}
if (empty($_POST["fhb_email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["fhb_email"];
}
$EmailTo = "junk@zenwebcreative.com.au";
$Subject = "Ebook Downloaded by $name";
$Body = "";
$Body .= "Name:\n";
$Body .= $name;
$Body .= "\n\n";
$Body .= "Email:\n";
$Body .= $email;
$Body .= "\n\n";
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
Thanks