populate hidden form field upon submit

Kim Tompkins asked on February 12, 2021 23:48

Is there a way to populate a hidden form field with data based upon a selection made in a drop down on the page. When the form is submitted the value of the drop down in in the URL example. http://xxx?form=4. I want to be able to read the value in the url and populate the hidden field on the form with that value.

Thank you

Correct Answer

Dmitry Bastron answered on February 17, 2021 10:15

Hi Kim,

The following code in .ascx should work:

<asp:HiddenField runat="server" ID="myHiddenField"/>

<script type="text/javascript">
    function getUrlParameter(name) {
        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
        var results = regex.exec(location.search);
        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
    };

    document.getElementById('<%# myHiddenField.ID %>').value =  getUrlParameter('yourUrlParameter');
</script>
0 votesVote for this answer Unmark Correct answer

Recent Answers


Dmitry Bastron answered on February 14, 2021 17:47

Hi Kim,

Yes, it is definitely doable. What version of Kentico are you using? Is it web forms or MVC? Are you talking about form builder functionality or a custom form? Is it a multistep form or a single form on a page? It's really hard to guess from your question.

0 votesVote for this answer Mark as a Correct answer

Kim Tompkins answered on February 15, 2021 16:15

Version 12. It's a web form. Custom not built with the builder. Single form on a page although there are others on the page as well. Thank you

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on February 16, 2021 10:59

Hi Kim,

You also mentioned that this hidden form field should be populated with a value from URL parameters, shouldn't it? In web forms you've got a couple of options then:

  1. On the backend: inside .ascx.cs (or .aspx.cs) where your form is located within OnLoad method you can read this URL parameter from the request and set a value for your hidden input.
  2. On the frontend: in javascript on page load read the parameter from URL and insert it in the hidden field value.
0 votesVote for this answer Mark as a Correct answer

Kim Tompkins answered on February 16, 2021 17:06

thanks for the response Dmitry. I guess what I'm not getting, and I know this is pretty basic, but how do I access the field and set the value in JavaScript. for instance the email field I can see in the source and it's always named something like this ( p$lt$ctl09$pageplaceholder$p$lt$ctl01$On_lineForm$viewBiz$Host_Email$txtText ) so I know how to access this field but if it's hidden the field is set to not be visible then it won't be in the source ? I know that I am just missing something very basic. Thank you for your time.

0 votesVote for this answer Mark as a Correct answer

Kim Tompkins answered on February 19, 2021 22:23

thank you

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.