Print Page | Close Window

QueryString parameters

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=15606
Printed Date: 30 March 2026 at 5:05am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: QueryString parameters
Posted By: theSCIENTIST
Subject: QueryString parameters
Date Posted: 27 June 2005 at 1:01am
Hi folks,

Been scratching my head on this one, and nothing, my problem is this:

We all pass stuff along using the querystring, however, how does one solve the problem of adding another parameter to the existing parameters in the querysting?

Response.Write(vbCrLf & "    <a href=""" & thisFile & "?" & Request.ServerVariables("QUERY_STRING") & "&shw=cds"" class='link'>Show all codes</a>")

This works for the first time, a [&shw=cds] is added to whatever is already there, but has I keep pressing the link more and more [&shw=cds] are added.

Any solutions?

-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com



Replies:
Posted By: dpyers
Date Posted: 27 June 2005 at 1:49am
Something like...
If  Lcase(Request.QueryString("shw")) <> "cds" Then
   Add to the QUERY_STRING
Else
   Use it as is.
End If



-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: theSCIENTIST
Date Posted: 27 June 2005 at 2:46am
Isn't there a more subtle way to do it?

That means all my links will have to have that check.

-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: dpyers
Date Posted: 27 June 2005 at 10:21am
If you're calling the same page, you might want to clear the request object somewhere in the page.

If you do have multiple entries for a parameter, I think it is referenceable as an array similar to multiple entried servervariables. If you don't specify an index value, it gets the first entry.

A query string has a limit of around 1K (for some reason I think it might be 968 characters). Data and parameter characters count towards the limit.
If the limit is an issue or if you just want to clean up the look of the url you might want to POST the info via a hidden form.

In .net you can use a url handler to automatically rewrite the url but still retain the request object.


-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: theSCIENTIST
Date Posted: 27 June 2005 at 11:21am
My app is a 1 file app, so I'm not calling and passing queries to other files, even so, I find it too awkward to have to add so much code just to preserve all queries already in the URL and add another query to it. Observe:

Old link:
Response.Write(vbCrLf & "    <a href=""" & thisFile & "?" & Request.ServerVariables

("QUERY_STRING") & "&shw=cds"" class='link'>Show all
codes</a>")

New link:
Response.Write(vbCrLf & "    <a href=""" & thisFile & "?" & Request.ServerVariables("QUERY_STRING") & "")

If LCase(Request.QueryString("shw")) <> "cds" Then Response.Write("&shw=cds")
Response.Write(""" class=""link"">Show all codes</a>")

For the new link I need 3 lines of code just to add a query string and preserve all others already in the URL?
Such a simple operation, isn't there an easier way?
I have hundreds of links in my file, and to change them all, the file will triple in size.

Ideas please.

-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: dj air
Date Posted: 27 June 2005 at 11:40am
what about

strQuerystring = Request.ServerVariables("QUERY_STRING")

if NOT intstr(1, Lcase(strQuerystring), "shw=cds", vbTextCompare)  then
strQuerystring = strQuerystring & "shw=cds"
END IF

you could even add it within a function so its url" & call function()

just an idea not sure any of that is helpful


Posted By: dpyers
Date Posted: 27 June 2005 at 12:13pm
Perhaps a different approach using session variables to avoid the querystring entirely?

-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: theSCIENTIST
Date Posted: 27 June 2005 at 1:59pm
Great ideas, a function or session vars, however can you post an example of the function & call approach, something so I can use the links as usual like; <a href="" & thisFile & "?" & smartQuery(shw=cds) & ">Codes</a>

Note smartQuery(shw=cds) is the function that would return a URL with all the old queries but also add to them a new shw=cds parameter and also calculate whether to insert it or if it's already ther don't insert it.

Can you give me an example of a function like this?

My app uses a great deal os queries whateverapp.com/project.asp?op=ac&bid=33&nc=0&bp=0&shw=cds so I don't know which approach to use.

I prefer not to use session variables as this project is a one file blog application that will be used by many people have their own blog, so session vars would conflict.

-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: dj air
Date Posted: 27 June 2005 at 2:28pm


Private Function SmartQuery(ByVal strquerystrings, ByVal strValues)

dim strQuerystring
dim i
dim saryquerystrings
dim strquerystrings
dim strValues
dim saryValues

strQuerystring = "&" & Request.ServerVariables("QUERY_STRING")

saryquerystrings = Split(strquerystrings, "&")
saryValues = Split(";" & strValues, ";")

FOR i = 0 to unbound(saryquerystrings)

if not intstr(1, LCase(strQuerystring) , LCase(saryquerystrings (i)) & "=" & LCase(saryValues(i)) , vbTextCompare) then

strQuerystring = strQuerystring & "&" & saryquerystrings (i) &"=" & saryValues(i) & ""

end if

next

    'Return Function
    SmartQuery = strQuerystring
End Function




right the above should, but not tested get the existing querystring , then using the querystring veriables and values it checks each querystring parameter (strquerystrings), to see if it is within the querystring if its not it then gets the value (strValues), and adds "strquerystrings (value)=strValues (value)" to the querystring value


to activate it use

 SmartQuery(strquerystrings, strValues)

SET strquerystrings to all the querystring parameters (shw)

SET strValues to all the wanted values for that querystring (cds)

seperate strQuerystrings with & (dont start with one but always end with one

seperate strValues with ;

so for example

SmartQuery("swh&bid&","cds;33;")


i thin that will work let me know


Posted By: theSCIENTIST
Date Posted: 28 June 2005 at 4:23pm
Thx, your function is good, but still don't address my needs, what I need is a way to make a link and only tell in that link the new querystring to use, always preserving any existing queries and checking for the presence of a query like the new one being put in.

Example, for paging, I use bp=6, it should remove bp=5 if it's there and only apply my new bp=6 always preserving all other queries in there.

Your function is good nevertheless, but with it I would still have to declare all queries in the link, what I want is to make a link like [<a href="" & thisFile & "?" & smartQuery("bp=" & bp + 1) & ">Next</a>] then the function smartQuery should:

- Gather all existing queries.
- Remove any bp=? already there and apply my new bp=?

See the point?

This is the first app I do which has alot of queries to be processed at any one time, I am the only one facing this problem?

I could offcourse declare all queries in all links, but then everytime I had a new query (like a new feature) I have to manually update all links to preserve that new query, too much trouble, there's gotta be a function controlling all this.

Help please.

-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: dj air
Date Posted: 29 June 2005 at 8:53am
ok, the below version gets the querystring, then does the checks on parameter, then if it exists it deletes it and its value then adds the new one.



Private Function SmartQuery(ByVal strquerystrings, ByVal strValues)



dim strQuerystring
dim i
dim saryquerystrings
dim strquerystrings
dim strValues
dim saryValues
dim strTempQueryValue

strQuerystring = "&" & Request.ServerVariables("QUERY_STRING")

saryquerystrings = Split(strquerystrings, "&")
saryValues = Split(";" & strValues, ";")

FOR i = 0 to unbound(saryquerystrings)

' delete the parameter and value from the querystring
if intstr(1, LCase(strQuerystring) , LCase(saryquerystrings(i)) &"=", vbTextCompare) then


strTempQueryValue = request.querystring(saryquerystrings(i))
strQuerystring = replace(LCase(strQuerystring), Lcase("&" & saryquerystrings(i)) & "=" & strTempQueryValue , "")


strQuerystring = replace(LCase(strQuerystring), Lcase("?" & saryquerystrings(i)) & "=" & strTempQueryValue &"&" , "?")


END IF

' insert the new parameter
strQuerystring = strQuerystring & "&" & saryquerystrings (i) &"=" & saryValues(i) & ""


next

    'Return Function
    SmartQuery = strQuerystring
End Function




you execute like before but now it checks wheather the parameter already exists within the querystring, if it does it deletes it and its value.

sorry  for the late reply.


Posted By: theSCIENTIST
Date Posted: 02 July 2005 at 9:10pm
Hi, no problem, I have tested your function and it works however I'm still required to declare all queries in the link, not what I want to do.

On each link I just want to declare 1 query by that link and then the function does calculate what other queries already exist and fix the repeats.

Basicaly, if the querystring is now: fun.asp?op=ab&bid=33&cid=21&bp=16&shw=ops and I press the paging button to go to the next page, it should then be: fun.asp?op=ab&bid=33&cid=21&bp=17&shw=ops only bp=17 changed all the rest remains the same, this paging button has only a call to the function("bp=17") and the function does the rest.

I have done some work on it, but I'm now a bit lost, check and run this please:

<%

thisFile = "fun.asp"

Response.Write(vbCrLf & "<a href=""" & thisFile & "?op=ab&bid=33&cid=21&bp=16&shw=ops"">Lotsof</a><br><br>")

Response.Write(vbCrLf & "<a href=""" & thisFile & """>None</a><br><br>")

Response.Write(vbCrLf & "<a href=""" & thisFile & "?" & sQuery("shw=cds") & """>Query</a>")


'// [=======================================================]
'// [=| sQuery                                           |=]
'// [=======================================================]
Function sQuery(ByVal strMyQuery)

Dim strQueryFull, arrVarAndValue, arrVar, arrMyVar, arrMyValue, query, var

strQueryFull = Request.ServerVariables("QUERY_STRING")

arrVarAndValue = Split(strQueryFull, "&")

'// Get my variable not value with arrMyVar(0)
arrMyVar = Split(strMyQuery, "=")

For query = 0 to UBound(arrVarAndValue)
    If LCase(arrVarAndValue(query)) = strMyQuery Then strMyQuery = ""
    Response.Write(vbCrLf & "<b>Query: " & arrVarAndValue(query) & "</b><br>")

    arrVar = Split(arrVarAndValue(query), "=")

    For var = 0 to UBound(arrVar)
      If var Mod 2 Then Response.Write(vbCrLf & "Var only: " & arrVar(var Mod 1) & "<br>")
    Next

    Response.Write("<br>")
Next


If strMyQuery = "" Then
    sQuery = strQueryFull
ElseIf strQueryFull = "" Then
    sQuery = strMyQuery
Else
    sQuery = strQueryFull & "&" & strMyQuery
End If

End Function
'// --------------------------| End of function: sQuery
%>


See what I need is to make a link like: Response.Write(vbCrLf & "<a href=""" & thisFile & "?" & sQuery("shw=cds") & """>Query</a>") -- Then the function will decimate what's already in the querystring by the & sign, then for each found it will seperate var/value by the = sign, then compare the var with the var I'm passing to the function and it is there already change only the value, so if the URL already contains and shw=ops and my link has shw=cds the old shw=ops should disappear in favor of my one.

-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: TrendSetterX
Date Posted: 23 July 2005 at 1:39pm

This code should solve all your problems, plus. The first method depends on the other two, which are equally useful on their own.  You'll need to remove the &nnbs p; that the board added in the one function.

/// <summary>
/// Appends string items together with a specific string separating the items. Unless false is
/// specified for AppenWhenListItemIsEmpty, an empty ListItem item will be appended.  If true is
/// specified for AppendWhenListItemIsNull, a null ListItem will be appended as an empty string.
/// </summary>
/// <param name="List">The list to append to</param>
/// <param name="ListItem">The item to be appended to the list</param>
/// <param name="ListItemSeparator">The string that will separate the list from the new item</param>
/// <param name="AppendWhenListItemIsNull">
/// If true, a null ListItem should be appened as an empty string. 
/// If false, a null ListItem will cause the List to be returned as-is without any modification.
/// </param>
/// <param name="AppendWhenListItemIsEmpty">
/// If true, an empty ListItem should be appened. 
/// If false, an empty ListItem will cause the List to be returned as-is without any modification.
/// </param>
/// <returns>A separated string</returns>
public static string AppendListItems(string List, string ListItem, string ListItemSeparator, bool AppendWhenListItemIsNull, bool AppendWhenListItemIsEmpty)
{
      string ret = string.Empty;
 
      if ((!AppendWhenListItemIsNull && ListItem == null) || (!AppendWhenListItemIsEmpty && ListItem.Trim().Length == 0))
      {
            ret = List;
      }
      else
      {
 
            // Validate the ListItemSeparator argument
            if (ListItemSeparator == null)
            {
                &nbs p; throw new System.ArgumentNullException("ListItemSeparator");
            }
 
            // Clean the ListItem argument
            if (ListItem == null)
            {
                &nbs p; ListItem = string.Empty;
            }
 
            // Append the list items
            if (List == null || List == string.Empty)
            {
                &nbs p; ret = ListItem;
            }
            else
            {
                &nbs p; if (List.EndsWith(ListItemSeparator))
                &nbs p; {
                &nbs p;       ret = List + ListItem;
                &nbs p; }
                &nbs p; else
                &nbs p; {
                &nbs p;       ret = List + ListItemSeparator + ListItem;
                &nbs p; }
            }
      }
     
      // Return the result
      return ret;
}
 
 
 
/// <summary>
/// Appends a name and value onto a querystring as a name=value pair.
/// </summary>
/// <param name="QueryString">The Querystring to add the items to</param>
/// <param name="Name">The Name part of the pair</param>
/// <param name="Value">The Value part of the pair</param>
/// <param name="ReplaceNameValuePairIfNameAlreadyPresent">Whether or not to replace the
/// name value pair with the new name value pair if the name already exists</param>
/// <param name="AppendValueToNameIfNameAlreadyPresent">Whether or not to include the
/// value as a subvalue of Name if Name already exists in the querystring.  If
/// ReplaceNameValuePairIfNameAlreadyPresent is true, this value is ignored</param>
/// <returns></returns>
public static string AppendNameValuePairToQueryString(string QueryString, string Name, string Value, bool ReplaceNameValuePairIfNameAlreadyPresent, bool AppendValueToNameIfNameAlreadyPresent)
{
      string ret = string.Empty;
 
      if (!(Name == null || Value == null || Name.Trim().Length ==0 || Value.Trim().Length == 0))
      {
            // Clean the querystring
            if (QueryString == null)
            {
                &nbs p; QueryString = string.Empty;
            }
            else
            {
                &nbs p; QueryString.Trim(' ', '&');
            }
 
            bool nameAlreadyPresent = (QueryString.IndexOf("?"+Name+"=") >= 0 || QueryString.IndexOf("&"+Name+"=") >= 0);
           
            // Add the value onto the end of the existing name=value pair with a comma between
            //  the added value and the previous value
            if (nameAlreadyPresent && (ReplaceNameValuePairIfNameAlreadyPresent || AppendValueToNameIfNameAlreadyPresent))
           


Posted By: theSCIENTIST
Date Posted: 23 July 2005 at 9:12pm
Hi,
Is it client side javascript?

I prefer not to use any client side javascript as it can be turned off, so rendering my app useless, I don't even do form validation that way any more for the same reason, and, I hate not being able to access a web site or link because it relies on javascript, which I always have off, and so most security consciencious people.

If it is server side javascript, all my code is in VBSCript so can't use it either.

Thanks anyway, appreciatted it.

-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: TrendSetterX
Date Posted: 23 July 2005 at 9:17pm
It's actually asp.net/c# code.  Thanks for appreciating... :)



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net