Home > Back-end >  How to add parameter in the html form action?
How to add parameter in the html form action?

Time:09-17

I have a form like this:

<form action="studyresults.show" method="POST">
   ...
   ...
   <div style="clear: both;float: right;padding-top: 15px;padding-bottom:5px;">
          <input id="submitButton" style="float:right;margin-top:-80px;" type="submit" class="btn btn-primary" value="Add" />
        </div>

I want to add a parameter from url string like this:

<script>
function getQueryStringValue (key) {
          return decodeURIComponent(window.location.search.replace(new RegExp("^(?:.*[&\\?]"   encodeURIComponent(key).replace(/[\.\ \*]/g, "\\$&")   "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
        }
</script>

I would like to query the url with "token" string so I can pass "token" as part of POST request parameter when I click submit button. How can I achieve this?

CodePudding user response:

Since you're using JSP, there's no need to involve JavaScript here. You can pull any query string parameter value directly using JSP's EL expression language.

<form action="studyresults.show" method="POST">
    <input type="hidden" name="token" value="${param.token}" />
    ...
    <div style="clear: both;float: right;padding-top: 15px;padding-bottom:5px;">
        <input id="submitButton" style="float:right;margin-top:-80px;"
               type="submit" class="btn btn-primary" value="Add" />
    </div>
</form>

The above example adds the token value ${param.token} as a hidden <input> field which will get POSTed along with other field values when the form is submitted.

  • Related