首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从ContactForm 7提交中获取数据?

如何从ContactForm 7提交中获取数据?
EN

Stack Overflow用户
提问于 2018-08-20 11:56:50
回答 1查看 0关注 0票数 0

问题陈述: 如何从ContactForm 7提交中获取数据?

这是我在互联网上找到的解决问题的方法:

代码语言:javascript
复制
<?php
    	
	$formTitle = $contact_form->title();
    $submission = WPCF7_Submission::get_instance();
	if ( $formTitle == "myFormName" && $submission ) {
		// get the actual data!
		$posted_data = $submission->get_posted_data();
		$firstName = posted_data['firstname'];
		$lastName = posted_data['lastname'];
	}
}
?>
EN

回答 1

Stack Overflow用户

发布于 2018-08-20 21:34:02

这是完整的工作代码。

代码语言:javascript
复制
// add contact form 7 hook 
add_action('wpcf7_before_send_mail', 'my_get_form_values');

function my_get_form_values($contact_form)
{
    if ($contact_form->id==11) //your contact form 7 id
    {
        // get info about the form and current submission instance
        $submission = WPCF7_Submission::get_instance();
        if ( $submission )
        {
            $posted_data = $submission->get_posted_data();

            my_generate_xml($posted_data);
        }
    }
}

function my_generate_xml($posted_data)
{
    // get a serial number from the contact form to distinguish report from others
    $serial_no = $posted_data['SerialNumber'];

    // create xml doc spec
    $xml_doc = new DOMDocument('1.0', 'UTF-8');
    $xml_doc->formatOutput = true;

    // build maximizer xml file, do not use special characters
    $xml_root = $xml_doc->createElement('bug_report_' . $serial_no);
    $xml_doc->appendChild($xml_root);

    // create node for current user
    $xml_user = $xml_doc->createElement('User');

    $xml_fn = $xml_doc->createElement('firstname', $posted_data['firstname']);
    $xml_user->appendChild($xml_fn);

    $xml_ln = $xml_doc->createElement('lastname', $posted_data['lastname']);
    $xml_user->appendChild($xml_ln);

    $xml_root->appendChild($xml_user);

    // save it as a file for further processing
    $content = chunk_split(base64_encode($xml_doc->saveXML()));

    $uploads = wp_upload_dir();     

    $xml_doc->save($uploads['basedir'].'/prorh/'.(int)microtime(true).'-ticket-'.$serial_no.'.xml');
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档