Quantcast
Channel: Topliners : All Content - All Communities
Viewing all articles
Browse latest Browse all 3423

Passing parameters from a URL to a hidden field

$
0
0

If you ever have the need to pass a parameter from a URL to a hidden field in a form, you can do it with a bit of Javascript.

 

First you need to create your hidden fields in your form:

 

<input type="hidden" name="fieldOne">

<input type="hidden" name="fieldTwo">

 

Then add the JS to your page:

 

<script>

    function FillForm() {

        var FormName = "elqForm";

        var qLoc = location.href.indexOf('?');

        if(qLoc < 0) { return; }

        var q = location.href.substr(qLoc + 1);

        var list = q.split('&');

        for(var i = 0; i < list.length; i++) {

            var kv = list[i].split('=');

            if(! eval('document.'+FormName+'.'+kv[0])) { continue; }

            kv[1] = unescape(kv[1]);

            if(kv[1].indexOf('"') > -1) {

                var re = /"/g;

                kv[1] = kv[1].replace(re,'\\"');

            }

       eval('document.'+FormName+'.'+kv[0]+'.value="'+kv[1]+'"');

       }

    }

    FillForm();

</script>

 

Make sure that the FormName variable matches the name value in your form:

 

<form name="elqForm" method="POST" action="your-action-url">

 

Goto a URL using parameters

 

http://www.yoursite.com/index.php?fieldOne=Work&fieldTwo=Play

 

View the Generated Source of your page (use Web Developer's Toolbar for this). Look for the values "Work" and "Play" in their respective hidden fields.


Viewing all articles
Browse latest Browse all 3423

Trending Articles