Javascript to add querystring url to hidden form fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ozzii
    New Member
    • Jan 2007
    • 37

    #1

    Javascript to add querystring url to hidden form fields

    Hi,

    Is there a way to add the infomation in a url querystring/link to hidden form fields
    and submit that form when the user clicks on the link?

    Basically I have some paging links generated from a search page which hold a lot of search data and other variables. I need to send this data using the post method as its too much to be sent via the get method employed using a query string.

    Any other technique to acheive the above would be appreciated as well. I've considered holding the data in session variables but if these could time out before the user has browsed through all their search results!
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You should probably do this on the server-side, but you can use the search property. Parse it and set the hidden form values to the search values.

    Comment

    • ozzii
      New Member
      • Jan 2007
      • 37

      #3
      Originally posted by acoder
      You should probably do this on the server-side, but you can use the search property. Parse it and set the hidden form values to the search values.
      I dont understand what you mean by doing it on the server side? the paging links/numbers are generated dynamicaly and hold search data in the querystring. When the user clicks on the navigation link the data in that link is so long that the querystring get method will simply truncate. It therefore needs to be sent via the post method. The only way i can think of doin this is through javascript. the paging link would have a onclick even which sends it to a function that adds the search variables to a hidden form and then submits it.

      How can you do that on the server side?

      Comment

      • ozzii
        New Member
        • Jan 2007
        • 37

        #4
        Originally posted by acoder
        You should probably do this on the server-side, but you can use the search property. Parse it and set the hidden form values to the search values.

        Ok I've come up with an idea. I could read all the variables in to one variable and then parse that at the other end. I have the following as example:

        Code:
        original query string:
        
        "mysearchresults.asp?foo=blah&faa=blah&fee=blah&numperpage=10&pagenum=2"
        
        myquery = "mysearchresults.asp?foo=blah&faa=blah&fee=blah&numperpage=10&pagenum=2"
        
        new url string:
        "mysearchresults.asp?query=myquery"
        How do i parse the variable myquery into name value pairs in ASP?

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          By doing it one the server-side, I meant dynamically generating from the server-side. If, however, you are using javascript, see the link I gave earlier. Parse the search string using string methods, e.g. split() which will split the string into an array (probably using "&" as a delimiter). Then get the form values and set them to the name-value pairs that you derive from the string.

          Comment

          • ozzii
            New Member
            • Jan 2007
            • 37

            #6
            Originally posted by acoder
            By doing it one the server-side, I meant dynamically generating from the server-side. If, however, you are using javascript, see the link I gave earlier. Parse the search string using string methods, e.g. split() which will split the string into an array (probably using "&" as a delimiter). Then get the form values and set them to the name-value pairs that you derive from the string.
            Ok i've found a script to parse the query string from a url however I get nothing in the parsed variable even thoug I know there is a value for that variable in the query string! Below is the code am using. any suggetions. Do I need to unescape the pair for it to work?

            The url link is : "mysearchresult s.asp?fpage=1" generated dynamically

            Code:
            <script>
            function getQueryVariable(variable) {
              var query = window.location.search.substring(1);
              var vars = query.split("&");
              for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                  return pair[1];
                }
              } 
              alert('Query Variable ' + variable + ' not found');
            }
            </script>

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Try this script.

              Comment

              Working...