0

I have a page that I want to create a drop down list, and post back to the same page that I am on now.

How can I do this?

One catch is, I want all the querystring values to be the same also, except for 1 which is what the drop down list will be overriding.

loyalflow
  • 14,275
  • 27
  • 107
  • 168

2 Answers2

2

You can use both querystring and form variables in the same page if you want. If you use <form method="post"> and leave the action empty, it will post the form back to the current page, so that's one problem solved. There is one caveat: I'm not sure if leaving the action empty will keep the querystring parameters intact or not.

If not, you can try something like this: <form method="post" action="index.asp?<%= request.querystring %>"> (not sure about the exact syntax, the gist is that you will need to specify the current page and add the current querystring variables in the method).

In the ASP code on your page after posting you can check both request.form and request.querystring. request.form will contain your form post variables. request.querystring will contain the variables behind the ? in your URL.

HTH, Erik

Erik Oosterwaal
  • 4,272
  • 3
  • 44
  • 64
-1

A Javascript method:

<html>
<head>
<script>
function jumpto(whatform, querykey) {
    //get the url querystring
    var url = window.location.search;
    //the replace query
    var queryrx = new RegExp("([?&])" + querykey + "=[^\&]+(?=&|$)", "gi");
    //which item selected in dropdown
    var index=whatform.pageselect.selectedIndex;
    //if the first option, ignore it since it is blank
    if (whatform.pageselect.options[index].value != "0") {
        //is a query string available
        if (url.length>0) {
            //our query key is present
            if (queryrx.test(url)) {
                //now we replace the querystring from old to new
                url = url.replace(queryrx, '$1' + querykey + '='+whatform.pageselect.options[index].value);
                //clear out the question mark from the querystring
                url = url.replace("?", '');
            //our query key is not present, but there is querystring data
            }else{
                url = url.replace("?", '');
                url = querykey + "=" + whatform.pageselect.options[index].value + "&" + url;

            }
        //no querystring data exists
        }else{
            url = querykey + "=" + whatform.pageselect.options[index].value;
        }
        //alert(url);
        //this is the url we are getting bounced to
        location = "mypage.asp?"+url;
    }
}
</script>
</head>
<body>
<FORM NAME="form1">
<SELECT NAME="pageselect" ONCHANGE="jumpto(this.form, 'thequerykey')" SIZE="1">
<OPTION VALUE="">Choose a Page</OPTION>
<OPTION VALUE="pageA">First Page</OPTION>
<OPTION VALUE="pageB">Second Page</OPTION>
</SELECT>
</FORM>
</body>
</html>

If you want to go with an ASP Classic solution, you will need to use a function to clear your old value from your querystring https://stackoverflow.com/a/1221672/2004151 And then print your querystrings as Hidden Input fields in your form (MyFunctionResultsExceptPageSelect below). Something like:

<FORM ACTION="mypage.asp" METHOD="GET" NAME="form3">
<%=MyFunctionResultsExceptPageSelect("pageselect")%>
<SELECT NAME="pageselect" ONCHANGE="document.form3.submit()" SIZE="1">
<OPTION VALUE="">Choose a Page</OPTION>
<OPTION VALUE="pageA">First Page</OPTION>
<OPTION VALUE="pageB">Second Page</OPTION>
</SELECT>
</FORM>

<%
Function MyFunctionResultsExceptPageSelect(key)

  Dim qs, x

  For Each x In Request.QueryString
    If x <> key Then
        qs = qs & "<input type=""hidden"" name="""&x&""" value="""&Request.QueryString(x)&""" />"
    End If
  Next

  MyFunctionResultsExceptPageSelect = qs

End Function
%>

If you want to get the current page instead of manually specifying it, then use the following. In javascript snippet, use the answer here: https://stackoverflow.com/a/5817566/2004151 And in ASP, something like this: http://classicasp.aspfaq.com/files/directories-fso/how-do-i-get-the-name-of-the-current-url/page.html

Community
  • 1
  • 1
roypd
  • 9
  • 2