dpfwhb7470 2017-07-20 17:33
浏览 57

联系表格7,发布数据以分离php文件

Im using CF7 to get user details. When user submit his form, i wanna get the input fields to my custom.php file and do some stuff in there.

I tried doing that with the js on_sent_ok: URL/custom.php?.....fields data.... but i think this is not the right method. But anyway that is working for me.

Is there a way to do that with hook action? I tried this.

function wpcf7_do_something (&$cfdata) {
   $goURL = 'http://contactform7.com';
   $cfdata->set_properties( array( 'additional_settings' => "on_sent_ok: \"location = '".$goURL."';\"" ) );
 }
 add_action("wpcf7_before_send_mail", "wpcf7_do_something");

I tried to echo something , triger a js console.log, and to redirect inside the wpcf7_do_something function but nothing is works. I really dont know if it works at all.

Is there a way to test if this action is working? Is there a way to redirect to onother location?

Thnx

  • 写回答

1条回答 默认 最新

  • douxuqiao6394 2017-08-04 20:39
    关注

    Once you forward the user after a successful submission, your form data is lost. You can intercept the form data processing in WP by hooking into the before_send_mail action hook provided by CF7. This allows you to access the form data on the server, preprocess it if necessary, and then POST the data to your custom processor script.

     // Create the new wordpress action hook before sending the email from CF7
    add_action( 'wpcf7_before_send_mail', 'my_conversion' );
       function my_conversion( $contact_form ) {
       $submission = WPCF7_Submission::get_instance();
    
      // Get the post data and other post meta values.
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    
      // these variables are examples of other things you may want to pass to your custom handler
        $remote_ip = $submission->get_meta( 'remote_ip' );
        $url = $submission->get_meta( 'url' );
        $timestamp = gmdate("Y-m-d H:i:s", $submission->get_meta( 'timestamp' ));
        $title = wpcf7_special_mail_tag( '', '_post_title', '' );
    
      // If you have checkboxes or other multi-select fields, make sure you convert the values to a string  
        $mycheckbox1 = implode(", ", $posted_data["checkbox-465"]);
        $mycheckbox2 = implode(", ", $posted_data["checkbox-466"]);
    
    
      // Encode the data in a new array in JSON format
      $data = json_encode(array(
          "posted_key_name_1" => "{$posted_data["input-name-1"]}",
          "posted_key_name_2" => "{$posted_data["input-name-2"]}",
          "posted_key_name_..." => "{$posted_data["input-name-..."]}",
          "posted_key_name_n" => "{$posted_data["input-name-n"]}",
          // any additional data to include that wasn't part of the form post?
          "From URL" => "$url",
          "From IP" => "$remote_ip",
          "Page Title" => "$title"
          ));
    
       // Finally send the data to your custom endpoint
            $ch = curl_init("https://www.YOURDOMAIN.com/custom.php");
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5); //Optional timeout value
            curl_setopt($ch, CURLOPT_TIMEOUT, 5); //Optional timeout value
            $result = curl_exec($ch);
            curl_close($ch);
    
        return $result;
          }
        }
      }
    

    This will post the selected form data to your custom processor right before CF7 processes the form and sends the confirmation mail. You'll still want to make sure the user experience is satisfactory by either displaying the CF7 confirmation message that the form has been submitted, or forwarding the user to a thank you page using the JS redirect -> on_sent_ok: https://yourdomain.com/thanks/

    If it is necessary that the user visits your custom processor page because the processor page generates information important to the user, you could package up all the form data into a URL string and append that onto the processing URL. Then, in your processing.php code, you'd use $_GET[] to access the data.

    See this article for details on how to dynamically update set the redirect URL: How to change contact form 7 Redirecting URL dynamically - WordPress

    Submitting data to a webhook code from this page: http://moometric.com/integrations/wp/contact-form-7-zapier-webhook-json-post/

    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题