Passing a variable to a querystring

Results 1 to 2 of 2

Thread: Passing a variable to a querystring

  1. #1
    Join Date
    Dec 1969

    Default Passing a variable to a querystring

    I have a simple form that passes a part code to an ASP page that queries a SQL database and returns a table of the stock in inventory.<BR>I have added a hyperlink querystring after the asp output that links to another ASP page. I want to pass the original partcode from the original form ("bvar" in the ASP page) to a new ASP page as a query string. This will create another set of results based on outstanding sales quantities for the part in question. When i pass the query string it picks up the variable as just a string and not the value it contains e.g.<BR><BR>original form input is fpart<BR>ASP1 accepts this as <BR>bvar = request.form("fpart")<BR>process the sql query and returns a table<BR>the query string on this results page is<BR>&#060;p&#062;&#060;a href="http://MYSERVER/so.asp?Q=bvar"&#062;Open Sales orders&#060;/a&#062;&#060;/p&#062;<BR><BR>However this just passes "bvar" as a literal string and response.write "Request.QueryString("Q")" returns bvar and not the partcode this variable contains.<BR>Is this the best way of persisting this variable to the next query or is there a better way (I&#039m sure there is) <BR>Any help, different approaches appreciated

  2. #2
    Join Date
    Dec 1969

    Default RE: Passing a variable to a querystring

    One thing that helps *A LOT* in understanding how ASP works is to do a "View &#124 Source" in the browser for each generated ASP page. Why? Because the browser *NEVER* sees you ASP code. It sees *only* the generated HTML. So it is going to do what the *HTML* tells it to do, not what you *think* your ASP is telling it.<BR><BR>If you did that, you would find that this line of ASP:<BR><BR> &nbsp; &#060;a href="http://MYSERVER/so.asp?Q=bvar"&#062;Open Sales orders&#060;/a&#062;<BR><BR>would generate this line of HTML:<BR><BR> &nbsp; &#060;a href="http://MYSERVER/so.asp?Q=bvar"&#062;Open Sales orders&#060;/a&#062;<BR><BR>Oops. No change, is there? The value of your ASP variable did *NOT* get sent to the browser, did it?<BR><BR>So it&#039s back to ASP fundamentals: What does your book tell you is the way to send the *VALUE* of an ASP variable to the browser? Don&#039t remember? We&#039ll all wait here while you look it up....<BR><BR>... tum tum tum tum tum tum tooo ... tum tum tum tum tink ta-diddly-diddly ...<BR><BR>[Theme from Jeopardy, if you couldn&#039t guess...]<BR><BR>Aha! Response.Write!!!!<BR><BR>So you *could* do this:<BR><BR> &nbsp; &#060;a href="http://MYSERVER/so.asp?Q=&#060;%<BR> &nbsp; Response.Write bvar <BR> &nbsp; %&#062;"&#062;Open Sales orders&#060;/a&#062;<BR><BR>And that *will* work!<BR><BR>Welll...maybe it will. What happens if the contents of "bvar" are anything other than just letters an numbers? Then you need to learn about something called Server.URLEncode. And you *could* use it thus:<BR><BR> &nbsp; &#060;a href="http://MYSERVER/so.asp?Q=&#060;%<BR> &nbsp; Response.Write Server.URLEncode(bvar)<BR> &nbsp; %&#062;"&#062;Open Sales orders&#060;/a&#062;<BR><BR>And that, too, works.<BR><BR>FINALLY...If you read a bit further in your ASP book (and if you don&#039t have one, RUN don&#039t walk and buy one), you&#039ll find there is a "shortcut" for Response.Write of a single expression. So you could code all that thus:<BR><BR> &nbsp; &#060;a href="http://MYSERVER/so.asp?Q=&#060;% = Server.URLEncode(bvar)%&#062;"&#062;Open Sales orders&#060;/a&#062;<BR><BR>Which might look confusing at first, but once you get used to it you&#039ll learn to love it for its simplicity and readability.<BR><BR>Hokay?<BR><BR>But you really do need to study the very basics of ASP [chapters 1, 2, and 3 or something like that] in a good book.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts