Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass Value of Clicked Link to a Second Page 2

Status
Not open for further replies.

symnow

Programmer
Apr 21, 2009
9
US
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.
 
pass the id of the record as a querystring value (each record in your db should have a unique id) to page 2. On page 2 read this value and qry the db accordingly. Once you have the value on page 2, you can store it however you like and pass it on to page 3.

What language do you normally program in, maybe I (or someone else) can give you an example of how it's done there and you can implement it in ASP.

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
I appreciate the response, but I am still confused. The links are populated on the first page from the database:

Code:
<% 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:

Code:
<script language="javascript">
  function fncSelected(){
  /// Open Report.asp for the product link selected
    window.open("[URL unfurl="true"]http://Report.asp",[/URL] "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.
 
Again, I'm going to assume you have a unique id for the strProduct, in your link, pass the id to the function

Code:
prod_id = rs("prod_id")
Response.Write ("onClick='fncSelected(" & prod_id & ")'")

and in your javascript, retreive the product id and pass it in the window.open event as a querystring.

Code:
<script language="javascript">
  function fncSelected(intInput){
  /// Open Report.asp for the product link selected
    window.open("[URL unfurl="true"]http://Report.asp?prod_id="[/URL] + intInput, "product");
}
</script>

On "report.asp" retrive the value from the querystring and qry the db accordingly.


Just out of curiosity, why are you using Javascript to open the new window, why not just pass it in the href tag and set the target to "_blank"?

Code:
<a href="mynewPage.asp?something=value" target="_blank">text</a>




TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
generating links:

Code:
<ul>
<%
  'whatever other checks here
  DO WHILE NOT rs.EOF
%>
<li><a href="yourpage2.asp?id=[COLOR=red]<%=rs("prod_id")%>[/color]" target="_blank"><%=rs("strProduct")%></li>
<%
  rs.MOVENEXT
  LOOP
%>
</ul>

this just generates your list - links & all. the (unique, i hope!) id of each record is put into the link (highlighted in red).


now, on your next page somewhere near the top (or before you wish to use the id):

Code:
<%
  [COLOR=red]prodID = request.querystring("id")[/color]
  'gets string after id=

  SQL = "SELECT * FROM prdtable WHERE ID = '" & [COLOR=red]prodID[/color] & "'"
  SET rs = myConn.execute(SQL)
  'we are using the product id as our where clause.
  'assumes you have opened the connection earlier.
%>
<p><%=rs("strProd")%></p>
...etc...

<%
  SET rs = NOTHING
  'close ocnnection if you want
%>
catches the querystring, uses that as the where clause (again in red) and then we fill out the page using the recordset.

as simple as i could get it

________________________________
Top 10 reasons to procrastinate:
1)
 
Thanks for the wonderful help. I'm making progress, but getting a few errors. Thanks for being patient too! It's been a long time since I've done this and memory isn't what it use to be.

So, using the code suggested by g0ste I receive the following error:

Item cannot be found in the collection corresponding to the requested name or ordinal.
/default.asp, line 69

Line 69 is:
Code:
<li><a href="Report.asp?id=<%=rs("pkeyIDProduct")%>" target="_blank"><%=rs("strProduct")%></a></li>

If I understand the error correctly, it's telling me the requested field is not in the dataset, but pkeyIDProduct is the unique ID for each product and is included in the SQL statement. The SQL statement:

Code:
sql="SELECT tblProduct.*, tblProduct.pkeyIDProduct FROM tblProduct WHERE tblProduct.strType=1 ORDER BY tblProduct.strProduct"
rs.Open sql, conn

If I change
Code:
..."Report.asp?id=<%=rs("pkeyIDProduct")%>"...
to
Code:
..."Report.asp?id=<%=rs("strProduct")%>"...
it works just fine and populates the page with the product links.

Then, once it's working on page 1, I again use the sample code from g0ste for page 2 and receive the following error:

Exception occurred.
/Report.asp, line 53

Here's the connection info and SQL:

Code:
<%
Set conn = Server.CreateObject("ADODB.Connection") 
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/Share/SAdbF02.mdb"))
set rs=Server.CreateObject("ADODB.recordset")
prodID = request.querystring("id")
sql="SELECT tblProduct.pkeyIDProduct, tblProduct.fkeyIDDesc, tblProduct.strProduct, tblProduct.strType, tblProduct.strHomeURL, tblProduct.strBuyURL, tblProduct.strDownloadURL, tblProduct.strPrice, tblProduct.memIntroDesc, tblProduct.strEmail, tblDesc.lngSort, tblDesc.strHead, tblDesc.fBullet, tblDesc.memDesc FROM tblProduct INNER JOIN tblDesc ON tblProduct.pkeyIDProduct = tblDesc.fkeyIDProduct WHERE tblProduct.strProduct='" & prodID & "' ORDER BY tblProduct.pkeyIDProduct, tblDesc.lngSort"
rs.Open sql, conn
%>

And line 53 is:

Code:
<%Response.Write("<a class='Report' style='font-size:12pt;' target='_blank' href='" & rs("strHomeURL") & "'>" & rs("strProduct") & "</a>")
%>

I'm not sure where I've gone wrong, and again, any and all help is greatly appreciated.
 
Code:
sql="SELECT tblProduct.*[COLOR=red], tblProduct.pkeyIDProduct[/color] FROM tblProduct WHERE tblProduct.strType=1 ORDER BY tblProduct.strProduct"

Here you are selecting everything - * - from the table. The red text is not needed here. That will probably resolve your item cannot be found... error - as this is calling the column pkeyIDProduct twice.

for more info see:


I think exception occurred is usually the end of & beginning of the recordset (i.e. no records in the db that match your criteria).

try sticking in a check for BOF & EOF criteria such as
Code:
IF rs.BOF AND rs.EOF THEN
  response.write("no records found!")
ELSE
  'do other stuff
END IF



________________________________
Top 10 reasons to procrastinate:
1)
 
woops, premature button press!

in the ELSE put in your link

Code:
...
ELSE
  <%Response.Write("<a class='Report' style='font-size:12pt;' target='_blank' href='" & rs("strHomeURL") & "'>" & rs("strProduct") & "</a>")
%>
END IF

________________________________
Top 10 reasons to procrastinate:
1)
 
Your suggestion worked like a charm on both counts. The first page works perfectly and I indeed received "no records found" on the second page, which lead me to question the sql. Previously, on the second page, prodID was set to strProduct; however, that's not whats being passed in the querystring, instead it's pkeyIDProduct that's being passed. When I change it in the SQL, I started receiving the following error:

Error Type:
Microsoft JET Database Engine (0x80040E07)
Data type mismatch in criteria expression.
/Report.asp, line 11

Line 11 is:
Code:
rs.Open sql, conn

And the SQL (line 10) is:

Code:
<%
...
prodID = request.querystring("id")
sql="SELECT tblProduct.pkeyIDProduct, tblProduct.fkeyIDDesc, tblProduct.strProduct, tblProduct.strType, tblProduct.strHomeURL, tblProduct.strBuyURL, tblProduct.strDownloadURL, tblProduct.strPrice, tblProduct.memIntroDesc, tblProduct.strEmail, tblDesc.lngSort, tblDesc.strHead, tblDesc.fBullet, tblDesc.memDesc FROM tblProduct INNER JOIN tblDesc ON tblProduct.pkeyIDProduct = tblDesc.fkeyIDProduct [COLOR=red] WHERE tblProduct.pkeyIDProduct='" & prodID & "' [/color]ORDER BY tblDesc.lngSort"
%>

I spent the majority of this afternoon researching the error to no avail. So, I'm back asking for help. Your assistance is greatly appreciated. I can actually see the light at the end of this tunnel. :)
 
You said previously:

If I change
Code:
..."Report.asp?id=<%=rs("pkeyIDProduct")%>"...
to
Code:
..."Report.asp?id=<%=rs("strProduct")%>"...

You should change this back to pkeyIDProduct, as you are trying to use the strProduct - which is a string - to find pkeyIDProduct - which is most likely a number.

That's like saying Bicycle is the same as the number 56 - which it clearly isn't ;) (unless we talk abstractions?)

Give it a go anyways, and see how you get on :]

________________________________
Top 10 reasons to procrastinate:
1)
 
I must have written it backwards, because I changed it from strProduct to pkeyIDProduct and received the "mismatch in criteria expression error". It was originally strProduct, which then produced the "no records found" per the code you suggested.

So, I'm confused since I am passing the pkeyIDProduct number (I can see it in the status bar on the first page... //...Report.asp?id=8) and picking it up with the request.querystring on the second page and then putting that variable into the SQL.

Code:
<%
...
prodID = request.querystring("id")
sql="SELECT tblProduct.pkeyIDProduct, tblProduct.fkeyIDDesc, tblProduct.strProduct, tblProduct.strType, tblProduct.strHomeURL, tblProduct.strBuyURL, tblProduct.strDownloadURL, tblProduct.strPrice, tblProduct.memIntroDesc, tblProduct.strEmail, tblDesc.lngSort, tblDesc.strHead, tblDesc.fBullet, tblDesc.memDesc FROM tblProduct INNER JOIN tblDesc ON tblProduct.pkeyIDProduct = tblDesc.fkeyIDProduct [COLOR=red]WHERE tblProduct.pkeyIDProduct='" & prodID & "'[/color] ORDER BY tblDesc.lngSort"
%>

Again, thanks for taking the time to help with this.
 
so to my understanding, now you are passing through the querystring:
...Report.asp?id=8

or is it:
...Report.asp?id=string product Name

Ideally, we want to see the record id (pkeyIDProduct is an identity column?) passed to the querystring. then we retrieve that id using the request.querystring.

also need to make sure that tblProduct.pkeyIDProduct = tblDesc.fkeyIDProduct match and produce only one result, or this will fail. I would suggest removing the JOIN and all corresponding table references to tblDesc. something like:

Code:
SQL = "SELECT * FROM tblProduct WHERE pkeyIDProduct = '" & prodID & "'"

and then do the same for the other table:
Code:
SQL = "SELECT * FROM tblDesc WHERE fkeyIDProduct = '" & prodID & "'"

and see if it fails on either of those.

________________________________
Top 10 reasons to procrastinate:
1)
 
It is indeed

...Report.asp?id=string product Name

So the product id is being passed in the querystring.

I will try your suggestion on the Report.asp page and let you know how it goes. :)

I am curious though; I have tested the SQL within the database to ensure the results and it works perfectly. Although, it was tested using an actual number rather than trying to extract a single record with a variable. So, wouldn't it also work on the asp page? Just a thought.

Cheers
 
Turns out there was a problem with the SQL... a data type mismatch on Report.asp.

Code:
SQL = "SELECT * FROM tblDesc [COLOR=RED]WHERE fkeyIDProduct = '" & prodID & "'"[/COLOR]

I removed the single quotes since the value being passed is a number and it works perfectly!

Thank you so much for all your help!

One last question; how do I now pass the prodID on to the third page? I have a link and when the user clicks it opens a window with the selected company's contact information. It is the same company as selected on the first page, so I need to pass the prodID on to the third page (Contact.asp).

I have the following in Contact.asp:
Code:
Set conn = Server.CreateObject("ADODB.Connection") 
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/Share/SAdbF02.mdb"))
set rs=Server.CreateObject("ADODB.recordset")
prodID = request.querystring("id")
sql="SELECT tblProduct.* FROM tblProduct WHERE tblProduct.pkeyIDProduct=" & prodID & ""
rs.Open sql, conn

Here's the script:
Code:
<script language="javascript">
function fncLnk(){
/// Target url for telephone image 
window.open("[URL unfurl="true"]http://Contact.asp"[/URL] "contact", "left=369, top=147, toolbar=no, location=no, menubar=no, directories=no, scrollbars=yes, height=300, width=300");
}
</script>

And here's the html:
Code:
<a href="#" name="contact" onClick="fncLnk()"><img class="contact" border="0" src="ContactCard.gif" title="Company contact information" WIDTH="38" HEIGHT="26"></a>

When I click the link, nothing happens. In the status bar, it displays .../Report.asp?id=10#. I have tried a number of things, including passing the prodID as a function parameter (below), but have had no success.

Code:
...
function fncLnk(prodID){
/// Target url for telephone image 
window.open("[URL unfurl="true"]http://Contact.asp"[/URL] + prodID "contact", "left=369, top=147, toolbar=no, location=no, menubar=no, directories=no, scrollbars=yes, height=300, width=300");
}
</script>

Opening a new window for the contact information page is the final piece of my puzzle. I'm sure I've overlooked something obvious and I truly appreciate your help.
 
Code:
window.open("[URL unfurl="true"]http://Contact.asp[/URL][b][COLOR=red]?id=[/color][/b]" + prodID "contact", "left=369, top=147, toolbar=no, location=no, menubar=no, directories=no, scrollbars=yes, height=300, width=300");

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
^ you also need a comma after prodID and before "contact" - I'm assuming "contact" is the name of the new window?

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
This seems to have brought me a step closer. The window opens, but I receive the following error:

Error Type:
Microsoft JET Database Engine (0x80040E10)
No value given for one or more required parameters.
/SpamAvenger013009/Contact.asp, line 11


In the window in which the error is displayed (opened by the script), the following URL shows in the address bar:


The window opens with specifications from the function, such as the window dimensions, just not the product id.

The variable & SQL on Contact.asp:
Code:
prodID = request.querystring("id")
sql="SELECT tblProduct.* FROM tblProduct WHERE tblProduct.pkeyIDProduct=" & prodID & ""
rs.Open sql, conn

The script on Report.asp:

Code:
function fncLnk(prodID){
/// Target url for contact image 
window.open("[URL unfurl="true"]http://Contact.asp?id="[/URL] + prodID, "contact", "left=369, top=147, toolbar=no, location=no, menubar=no, directories=no, scrollbars=yes, height=300, width=300");
}

Yep, "contact" is the name of the new window.

So, it does not appear to be picking up the id. On the Report.asp page, when I mouse over the contact image (the link with the function call), the status bar displays: /Report.asp?id=10# (or which ever id associated with the product selected).

Thanks for the assistance. :)
 
how are you calling the function "fncLnk"

Also, might just be a typo, you don't need the quotes and ampersand at the end of your sql string assignment:

sql="SELECT tblProduct.* FROM tblProduct WHERE tblProduct.pkeyIDProduct=" & prodID & ""

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
he is calling fncLink thru an onclick="... event.

however, you do need the ampersand & quotes - because the SQL string will read:

"SELECT tblProduct.* FROM tblProduct WHERE tblProduct.pkeyIDProduct=19rs.Open sql, conn...

the initial " breaks out of the SQL string back into ASP, the & attaches whatever follows it - in this case, the prodID, then another ampersand to attach another " to get back IN the sql string for the third " to close the sql string.

try this HTML to remove the # being appended to the querystring:
Code:
<a href="[COLOR=red]javascript:void(0);[/color]" name="contact" onClick="fncLnk()"><img class="contact" border="0" src="ContactCard.gif" title="Company contact information" WIDTH="38" HEIGHT="26"></a>

I assume that the link you had before - using a null (#) was appending that to your querystring: so prodID would then actually be (for example) 81# which doesn't exist in the DB.

try using this JS:
Code:
function fncLnk(prodID){
/// Target url for contact image
window.open("[COLOR=red]Contact[/color].asp?id=" + [COLOR=red]<%=prodID%>[/color], "contact", "left=369, top=147, toolbar=no, location=no, menubar=no, directories=no, scrollbars=yes, height=300, width=300");
}

see if any of that helps.


________________________________
Top 10 reasons to procrastinate:
1)
 
Life savers... both of you! Thanks so much for the help. All three pages are running smoothly. Have a great weekend and thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top