Log in

View Full Version : Pass Value of Clicked Link to a Second Page



symnow
04-21-2009, 07:18 PM
I have three basic pages that are populated with information from a database. The first page contains a list of links. When the user selects a link, a second page is opened and certain fields are populated with the product information specific to the link selected.

I have written all the code to populate the fields on each of the pages. So right now, the first page correctly displays all of the links, but I am confused as to how to capture the user's selection and pass that on to the second page to display the requested product information.

Currently, If I open the second page directly (rather than attempting to select a link on the first page) the fields are populated with the first record in the database.

I also need to pass the same value to the third page as was passed to the second page.

I am using VB for the server side scripting and my host does not support global.asa files. The database is MS Access. I'm sure this question has been asked before, but I've been unable to find anything by searching. Any help will be greatly appreciated.

mo3s
04-22-2009, 05:44 AM
Yo,

I think the best approach here would be to specify what you want passed to the next page in the link and then get the value using Request.Querysting.

Say you want to pass the productid to the next page and you have three products with id's in the db 1,12 and 15.

Create these links:

www.mylink.asp?id=1
www.mylink.asp?id=12
www.mylink.asp?id=15

On the www.mylink.asp page you can do this (from the top of my head, not tested code):
Dim productid As integer
productid = Request.Querystring("id")

You now have the productid and you can use it in your query.

Did this help?

symnow
04-22-2009, 06:13 PM
I appreciate the response, but I am still confused. The links are populated on the first page from the database:



<% If rs.Fields("strType") = 1 Then
Do While not rs.EOF
Response.Write ("<li><a href='#' name='product'")
Response.Write ("onClick='fncSelected()'")
Response.Write rs("strProduct")
Response.Write ("</a></li>")
rs.MoveNext
Loop
End If
rs.Close
set rs=Nothing %>


This works in that it does populate the page with the links from the database.

And I have set up a function to pass the variable, but that's where I get stumped. I'm not sure how to set up the variable and then pick it up on the next page. Here's the function:


<script language="javascript">
function fncSelected(){
/// Open Report.asp for the product link selected
window.open("http://Report.asp", "product");
}
</script>


I think I should use request.querystring, but there are still gaps in my understanding of how to accomplish this. I'm not even sure that using the onClick event is the right way to accomplish this.

Thanks so much for the help. I've tried so many different things that I just can't seem to get a clear perspective on it myself.

mo3s
04-26-2009, 06:07 AM
I think this should do the trick. I pass the id form the db to your javascript funtion.

<% If rs.Fields("strType") = 1 Then
Do While not rs.EOF
Response.Write ("<li><a href='#' name='product'")
Response.Write ("onClick='fncSelected(""" & rs("id").Value & """)'")
Response.Write rs("strProduct")
Response.Write ("</a></li>")
rs.MoveNext
Loop
End If
rs.Close
set rs=Nothing %>

Here I glue the id to your url

<script language="javascript">
function fncSelected(id){
/// Open Report.asp for the product link selected
window.open("http://Report.asp?id=" + id, "product");
}
</script>


Then on your receiving page you do this:
Dim intID As Integer
intID = request.querystring("id")

>I'm not even sure that using the onClick event is the
>right way to accomplish this.
Well you can also just write you links in your asp function but when you want to open a new window then you have a good approach (I think).

>Thanks so much for the help.
Hope this helps. I did not test the code above but from the top of my head it should work.

Cheers