将php变量添加到自定义表单提交

时间:2016-12-06 作者:Rod0n

我正在使用联系人表单7插件,让用户根据提交的(表单)和服务器提供的数据生成pdf。

我还想在提交后显示一个“预览”,所以我需要将自定义字段传递给客户端,以便在一些js文件中获得它们。

这就是我所拥有的:

呈现pdf的插件:

<?php

add_action(\'wpcf7_before_send_mail\', \'generate_pdf\');
function generate_pdf($wpcf7) {

    $file_uri = \'fpdf/fpdf.php\';
    require_once($file_uri);

    /* PDF file initialization */
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->AliasNbPages();
    $pdf->SetFont(\'Arial\',\'B\',12);
    $today_date = "California, " . date("d  F  Y");
    $pdf->Cell(0, 10, $today_date, 0, 1, \'R\');
    $name = $data[\'your-name\'];
    $submission = WPCF7_Submission::get_instance();
    $data = $submission->get_posted_data();

    $pdf->Output(wp_upload_dir()[\'basedir\'] . \'/\' . $name . \'.pdf\', \'F\');
    $wpcf7[\'custom_field\'] = \'CUSTOM VALUE\';
    return $wpcf7;
}
?>
表单提交时调用的javascript文件:

$(\'.wpcf7-submit\').on(\'click\', function (e) {
        var data = $(\'form\').serializeArray();
        var cleaned_data = {};
        for (item in data) {
            var name = data[item][\'name\'];
            if (name[0] != \'_\'){
                cleaned_data[name] = data[item][\'value\']; 
            }
        }
        var testInput = cleaned_data["your-name"];


})(jQuery);
在最后一段代码中,我想获取php脚本传递的值,但我不知道如何实现。

1 个回复
SO网友:filipecsweb

我可能会给你一个解决方案。

但我不得不说,我不确定我是否理解你想要什么。

下面是我一步一步地设计的:

用户将访问一个页面,需要在其中插入电子邮件才能接收pdf文件wp_mail() 功能,以便用户可以在其收件箱中接收pdf


function my_script() { ?>

<script>
    ;(function () {

        $ = jQuery.noConflict();

        var form_data;

        // $(parent).on(event, selector, function)
        $(\'body\').on(\'submit\', \'form.wpcf7-form\', function( e ){
            e.preventDefault();

            // Keep all data posted through the form. Custom data can be handled later.
            form_data = $(this).serialize();

            $.ajax({
                method : \'post\',
                url : \'<?php echo get_stylesheet_directory_uri() . \'/custom.php\'; ?>\', // Put here your own php file path.
                dataType : \'json\',
                data : {
                    form_data : form_data,
                },
                success : function( response ) {

                    // Here u can do whatever u want with the response. This is what u asked for in your question.
                    // In your case the response will have data for u to build the preview.
                    // Remove the log below when running in production.
                    console.log( response );

                },
                error: function( xhr, status, error ) {

                    // Use this in case you are having erros and u want to see what\'s going on in your php file which is being requested by your AJAX.
                    // var err = eval( "(" + xhr.responseText + ")" );
                    // console.log( err.Message );

                }
            });
        });

    })();
</script>

<?php
}

add_action( \'wp_footer\', \'my_script\', 100 );

现在我们需要处理php文件。请参见上面的代码正在调用一个名为custom的文件。php。你可以给它取任何你想要的名字,只要确保你会为它使用正确的路径。请参见下面的“我的习惯”。php:


<?php
// We need to include wp-load.php in case we need to use WordPress functions and constants, and we will.
$absolute_path = explode( \'wp-content\', $_SERVER[\'SCRIPT_FILENAME\'] );
require_once $absolute_path[0] . \'wp-load.php\';

// Set response var.
$r = array();

// Optionally u can get rid of some unnecessary part of the serialzed string.
$_POST[\'form_data\'] = preg_replace( "/.+_wpnonce=.+&/U", \'\', $_POST[\'form_data\'] );

$fields = explode( \'&\', $_POST[\'form_data\'] );

foreach ( $fields as $field ) {

    $field_pieces = explode( \'=\', $field );

    // $r[\'form_data\'][\'your-name\'] = \'Your name\'.
    $r[\'data\'][$field_pieces[0]] = urldecode( $field_pieces[1] );

}

// If needed, set all your custom data here.
// You can use this custom data within the pdf and also u can put it in your preview.
$r[\'data\'][\'custom_field\'] = \'CUSTOM VALUE\';

// Require PDF library. Pay attention not to get the wrong path, otherwise this file will retrieve an error.
require_once \'fpdf/fpdf.php\';

/* Init PDF */
$pdf = new FPDF();
$pdf->AddPage();
$pdf->AliasNbPages();
$pdf->SetFont(\'Arial\',\'B\',12);
$today_date = "California, " . date("d  F  Y");
$pdf->Cell(0, 10, $today_date, 0, 1, \'R\');

// Make sure this path is right.
$pdf->Output(wp_upload_dir()[\'basedir\'] . \'/\' . $r[\'data\'][\'your-name\'] . \'.pdf\', \'F\');

function send_email() {

    global $r;

    // Here we are sending the pdf to the e-mail which was put by the user.
    $to = $r[\'data\'][\'your-email\'];

    // U can add more headers if u wish. Just google it.
    $headers  = \'From: Rod0n \' . "\\r\\n";

    $subject = \'Your requested PDF has arrived\';

    $msg = \'You can see your pdf attached. Thanks.\';

    // Attach the pdf. e.g. array( WP_CONTENT_DIR . \'/uploads/\' . $r[\'data\'][\'your-name\'] . \'.pdf\' );
    $attachment = array( \'put here the path for the pdf\' );

    wp_mail( $to, $subject, $msg, $headers, $attachment );

}

send_email();

// Encode our response in order to retrieve it subsequently.
$r = json_encode( $r );

// Retrieve response var.
echo $r;

请注意这两个代码片段,尤其是注释,并设置您自己的更改。

相关推荐