简体   繁体   English

根据URL参数填充隐藏的输入表单字段

[英]populate hidden input form fields based on URLparameters

I have a ppc landing page that I want to change the hidden value of a form field based on the URL parameter. 我有一个ppc登陆页面,我想根据URL参数更改表单字段的隐藏值。

The field

  <input id="Campaign_ID" name="Campaign_ID" type="hidden" value="7g012455dv5441vdf"> 

The URL would be something like mysite.com/?campaignidvalue= 7g012455dv5441vdf 该网址将类似于mysite.com/?campaignidvalue= 7g012455dv5441vdf

There will be other field "values" that are also based on the URL parameter, so it has to tie the "input id" (or name) to that specific value. 还将存在其他也基于URL参数的字段“值”,因此它必须将“输入ID”(或名称)绑定到该特定值。

EDIT: There was an error in my code. 编辑:我的代码中有错误。

PHP would be the easiest! PHP将是最简单的!

<?php       
        if(!empty($_GET['campaignidvalue'])) 
        {       
           echo '<input id="Campaign_ID" name="Campaign_ID" type="hidden" value="'. $_GET['campaignidvalue'].'"/>';
        }

?>

This block will check to see if the value is passed in a GET parameter, and then echo it out into a hidden form field. 该块将检查该值是否在GET参数中传递,然后将其回显到隐藏的表单字段中。

The first part: 第一部分:

if(!empty($_GET['campaignidvalue'])) 

Is checking to see if a parameter named Campaign_ID is being passed. 正在检查是否传递了名为Campaign_ID的参数。

So your URL of: 因此,您的URL:

mysite.com/?campaignidvalue=7g012455dv5441vdf

Would allow the code to continue, as it is not empty. 将允许代码继续,因为它不是空的。

This section is the part that actually displays it on the page. 此部分是实际在页面上显示的部分。

echo '<input id="Campaign_ID" name="Campaign_ID" type="hidden" value="'. $_GET['campaignidvalue'].'"/>';

If you view the source of the HTML page, you would see 如果您查看HTML页面的源代码,则会看到

By using this function: 通过使用此功能:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.search);

    if(results == null) {
        return "";
    } else {
      return decodeURIComponent(results[1]);
    }
}

You'll be able to get any URL parameter by name, like so: 您将可以按名称获取任何URL参数,如下所示:

var cid = getParameterByName(campaignidvalue);

Then, once you have the value you can use jQuery to set it, like so: 然后,一旦有了值,就可以使用jQuery进行设置,如下所示:

$(function() {
   $("#Campaign_ID").val(cid);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM