用户提交表单

时间:2016-11-21 04:19:39

标签: javascript php forms pdf

我希望访问者在点击表单提交按钮后下载PDF。

PDF应自动启动"另存为对话框"按下“下载”按钮并发送表单详细信息。

我正在使用Javascript将详细信息发送到PHP表单处理器。我不是这两种语言的专家,我把代码放在网上找到的各种例子中。

简而言之,我不知道如何在表单提交后自动启动下载,我不知道是否应该使用PHP或Javascript来实现它。

提前感谢任何帮助或建议。感谢

这是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);
    }

这是PHP处理代码

$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);

由于

1 个答案:

答案 0 :(得分:0)

答案取决于您分享的截图和评论。

在服务器端,您需要删除打印" success"的行。您只需将文件作为响应返回。

在客户端,更新您的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){
            // Whatever processing you need to do here
            formSuccess1();
        }
    });
}

请注意,您必须根据您的功能检查功能。我只想强调您不需要检查text == "success"