JQuery Ajax从Contact Form 7 Multi-Step Form Wordpress发送数据

时间:2018-05-30 10:19:26

标签: php jquery ajax wordpress contact-form-7

我想从php文件中的Contact Form 7 Multi-Step Form发送数据。每一步都有1-4个字段。所以我希望通过电子邮件发送的相同数据也发送到php文件。 发送到电子邮件的数据是:

Internet: [radio-internet]
Mobile: [radio-mobile]
Provider: [radio-provider]
Surname: [your-surname]
Name: [your-name]
Email: [your-email]
Phone: [your-phone]
Hours: [radio-hours]

我尝试过类似的东西但不起作用:

<script type="text/javascript">
document.addEventListener( 'wpcf7submit', function( event ) {
    //Form
    var inputs = event.detail.inputs;

    for ( var i = 0; i < inputs.length; i++ ) {
    if ( 'radio-internet' == inputs[i].name &&  'radio-mobile' == inputs[i].name &&  && 'radio-provider' == inputs[i].name && 'your-surname' == inputs[i].name &&  'your-name' == inputs[i].name  &&  'your-email' == inputs[i].name  &&  'your-phone' == inputs[i].name  &&  'radio-hours' == inputs[i].name ) {
        jQuery.ajax({
            url  : 'process.php', // the url where we want to POST 
            type : 'post',
            data :  inputs,
            success : function( response ){
                console.log(response);
                console.log('lead inserted');
            },
            error : function(e1, e2, e3){
                console.log(e1);
                console.log(e2);
                console.log(e3);
            }
        });
        console.log( event.detail );
    }

}, false );
</script>

注意:每个字段必须转到php文件中的不同字段

$data = array(
"lastname" => "" . $_POST['surname'], //surname
"firstname" => "" . $_POST['name'], //name
"mobile" => "" . $_POST['phone'], //mobile
"email" => "" . $_POST['email'], //email
);

2 个答案:

答案 0 :(得分:0)

为此,您可以在发送电子邮件之前使用联系表格7。

现在我只是将数据存储在一个文本文件中,您可以在此处添加逻辑。

 add_action( 'wpcf7_before_send_mail', 'd_before_send_email' );

 function d_before_send_email($cf7) {
    $output = "";
    $output .= "Name: " . $_POST['name'];
    $output .= "Email: " . $_POST['email'];       

    file_put_contents("contactform7output.txt", $output);
   }

答案 1 :(得分:0)

尝试使用'wpcf7mailsent'事件,它适用于我。

document.addEventListener( 'wpcf7mailsent', function( event ) {

            var inputs = event.detail.inputs;


                var username = document.getElementById('username').value; // OR jQuery( "#username" ).val();

                var email = document.getElementById('email').value; // OR jQuery( "#email" ).val();


                jQuery.ajax({
                   url  : <?php echo admin_url('admin-ajax.php'); ?>, // the url where we want to POST 
                type : 'post',
                data :  {action:second_form,username:username,email:email},
                success : function( response ){
                    console.log(response);

                },
                  error: function(response) {
                    console.log(response);
                  },
                });
                alert('hi');


        }, false );

Ajax动作函数

add_action('wp_ajax_second_form' , 'second_form');
add_action('wp_ajax_nopriv_second_form','second_form');
function second_form(){
echo $_POST['username'];
echo $_POST['email'];
//do for second form
}