php 联系表格 7 使用获取请求设置字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24060547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:08:17  来源:igfitidea点击:

Contact form 7 set field value with get request

phphtmlwordpressformscontact-form-7

提问by Meneer Venus

I want to set a field value with get request

我想用 get 请求设置一个字段值

example.com/subscribe/?email=asfafs

example.com/subscribe/?email=asfafs

but when i load the page which has the form on it, the form does not show. I get why it could not be showing. Because the form itself could submit a get request as well. I also installed thisplugin which should enable me to set a field value, but it does not.

但是当我加载上面有表格的页面时,表格没有显示。我明白为什么它不能显示了。因为表单本身也可以提交 get 请求。我还安装了这个插件,它应该能让我设置一个字段值,但它没有。

this is what i have:

这就是我所拥有的:

<p>Uw naam (verplicht)<br />
    [text* input-name] </p>

<p>Uw email (verplicht)<br />
    [dynamictext dynamicname "CF7_GET key='email'"] </p>
<p>Onderwerp<br />
    [text your-subject] </p>

<p>Uw bericht<br />
    [textarea your-message] </p>

<p>[submit "Verzenden"]</p>

In my page:

在我的页面中:

<?php echo do_shortcode('[contact-form-7 id="1062" title="Contactformulier 1"]'); ?>

I wouldn't mind using a different plugin for this. If there is one which suits my needs please tell me.

我不介意为此使用不同的插件。如果有一款适合我的需求,请告诉我。

采纳答案by john

I just installed the plugin you linked and tested it. The plugin isn't meant to pull in a GET variable for a field within Contact Form 7. The plugin will do two things

我刚刚安装了您链接的插件并对其进行了测试。该插件并不意味着为 Contact Form 7 中的字段引入 GET 变量。该插件将做两件事

  1. It will grab the $_GET variable and create a hidden field with it.
  2. It will show the variable on the page (just as text, not in a field)
  1. 它将获取 $_GET 变量并用它创建一个隐藏字段。
  2. 它将在页面上显示变量(就像文本一样,而不是在字段中)

The shortcode that you have in your example is for use by this http://wordpress.org/plugins/contact-form-7-dynamic-text-extension/plugin. I downloaded and tested that plugin as well, and it seems to work just fine.

您示例中的短代码供此http://wordpress.org/plugins/contact-form-7-dynamic-text-extension/插件使用。我也下载并测试了该插件,它似乎工作得很好。

Here is the page I created the example on. http://jtwebb.com/stackoverflow-question/?someemail=asdfif you want to take a look to see it is working with the dynamic-text-extension plugin.

这是我在其上创建示例的页面。http://jtwebb.com/stackoverflow-question/?someemail=asdf如果你想看看它是否与动态文本扩展插件一起工作。

UPDATE: This is my contact form 7 code:

更新:这是我的联系表格 7 代码:

<p>Your Name (required)<br />
    [text* your-name] </p>

<p>[showparam someemail] <-- this is the shortcode of show param, just text no field</p>

<p>[getparam someemail] If you inspect this you'll see a hidden get field with the value of 'someemail' in it.</p>

<p>Your Email (required)<br />
    [dynamictext* dynamictext-380 "CF7_GET key='someemail'"]<br>This field was created with <a href="http://wordpress.org/plugins/contact-form-7-dynamic-text-extension/">http://wordpress.org/plugins/contact-form-7-dynamic-text-extension/</a></p>

<p>Subject<br />
    [text your-subject] </p>

<p>Your Message<br />
    [textarea your-message] </p>

<p>[submit "Send"]</p>

回答by mehedi101

[text* your-name default:get]The field will obtain its default value from the GETvariable with the same name (“your-name”). Try this by accessing the URLof the page of the form with an additional query string:

[text* your-name default:get]该字段将从GET具有相同名称(“您的姓名”)的变量中获取其默认值。通过使用URL附加查询字符串访问表单页面的 来尝试此操作:

http://example.com/contact/?your-name=John+Smith

http://example.com/contact/?your-name=John+Smith

But what if you have two or more default options in a single form-tag? Let's consider this form-tag's case:

但是,如果您在单个表单标签中有两个或多个默认选项怎么办?让我们考虑这个表单标签的情况:

[text* your-name default:get default:post_meta "Your Name"]

[text* your-name default:get default:post_meta "Your Name"]

回答by Javacadabra

I know this question has already been answered but for anyone looking for a solution which doesn't require a plugin I opted to do the following.

我知道这个问题已经得到解答,但对于任何寻找不需要插件的解决方案的人,我选择执行以下操作。

First I created my form from within the plugin via the Wordpress dashboard. I added the field I wanted to hold the parameter from URL and assigned it an ID.

首先,我通过 Wordpress 仪表板从插件中创建了我的表单。我添加了我想保存来自 URL 的参数的字段并为其分配了一个 ID。

[text page-name class:form-control id:source minlength:2 maxlength:80]

[text page-name class:form-control id:source minlength:2 maxlength:80]

Next I added a link which would pass the parameter to the form like so

接下来我添加了一个链接,它将参数传递给表单,就像这样

<a href='http://mycoolwebsite.com/contact?id=homepage'>Contact Us</a>

<a href='http://mycoolwebsite.com/contact?id=homepage'>Contact Us</a>

Then using some Javascriptand JQueryI get the idparameter from the URL and set it as the value for my input in the form. (The getParameterByName(name,url)function was taken from this answer: How can I get query string values in JavaScript?)

然后使用 someJavascript并从 URLJQuery获取id参数并将其设置为表单中输入的值。(该getParameterByName(name,url)函数取自这个答案:如何在 JavaScript 中获取查询字符串值?

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    url = url.toLowerCase(); 
    name = name.replace(/[\[\]]/g, "\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var param = getParameterByName('id');
jQuery('#source').hide();
jQuery('#source').prop('disabled', true);
jQuery('#source').attr('value', param);
jQuery('#source').val(param);

I also hide and disable the input field so it is not seen and cannot be modified (easily). I also hide the input field using CSS

我还隐藏和禁用了输入字段,因此它不会被看到并且不能被修改(很容易)。我还使用隐藏输入字段CSS

#source{visibility:hidden}

#source{visibility:hidden}

This way I can link to the form from any where within my site and append the source of where the person came from and place it in the email that I get.

通过这种方式,我可以从我网站内的任何位置链接到表单,并附加此人的来源并将其放入我收到的电子邮件中。

I don't see any problems with this method and it removes the need to use a plugin. I'm sure it's not ideal to depend on Javascript but equally it's not ideal to use to many plugins on your site as they can become outdated very quickly and often can cause conflicts between one another.

我没有看到这种方法有任何问题,它消除了使用插件的需要。我确信依赖 Javascript 并不理想,但同样不理想的是使用您网站上的许多插件,因为它们很快就会过时并且经常会导致彼此之间的冲突。

Hope this helps anyone looking for an alternative. I'd like to know peoples opinions on this way as I'm open to suggestions on how to improve it.

希望这可以帮助任何寻找替代方案的人。我想通过这种方式了解人们的意见,因为我愿意接受有关如何改进它的建议。

回答by Mario62RUS

You can initially in the form field set a unique text, and then use the hook to replace it with the desired value. And then no plugin will be needed.

您可以最初在表单字段中设置一个唯一的文本,然后使用钩子将其替换为所需的值。然后就不需要插件了。

Example. In form code:

例子。在表单代码中:

<p>Phone<br />
[text phone "PHONE_VALUE"] </p>

in functions.php:

在functions.php中:

add_filter( 'wpcf7_form_elements', function( $form ) {
  $form = str_replace( 'PHONE_VALUE', $_GET['phone'], $form );
  return $form;
} );

in URL:

在网址中:

example.com/page?phone=111111

回答by buzibuzi

as per this article

根据这篇 文章

if you want to send a value to a select list, this can be done easily by adding the parameter default:getto the field code:

如果要将值发送到选择列表,可以通过将参数添加default:get到字段代码来轻松完成:

[select* the-recipient default:get "Employee One|[email protected]" "Employee Two|[email protected]" "Employee Three|[email protected]"]

and then send the parameter in the GET request, like:

然后在 GET 请求中发送参数,如:

http://yourdomain.com/contact/?the-recipient=Employee%20Two

回答by Nicolas R

If you have any specific logic for each form elements you can also use the wpcf7_form_taghook

如果您对每个表单元素有任何特定的逻辑,您也可以使用wpcf7_form_tag钩子

// define the wpcf7_form_tag callback
function filter_wpcf7_form_tag( $scanned_tag, $replace ) {

    // set default option for your field
    if($scanned_tag['name'] == 'my-field' && isset($_GET['my-param'])){
        $scanned_tag['options'][] = sprintf('default:%s', $_GET['my-param']);
    }

    // repeat operation for another field

    return $scanned_tag;
};

// add the filter
add_filter( 'wpcf7_form_tag', 'filter_wpcf7_form_tag', 10, 2 );