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!

Sending .asp variables to a Javascript open.window

Status
Not open for further replies.

walderr

Programmer
Jul 18, 2003
32
GB
I have a search engine where the user submits a form, and the results are taken from a database and then a summary is displayed. The user then clicks on the summary to open a javascript window containing a form with the full results for that particular record.

The problem is, that no matter how many results are displayed in the summary, whichever one you click on, only the datasheet for the first result returned is shown in the new window. I have got this to work when opening in a normal window, but wanted to use javascript to have a standard sized window. The relevant bit of code is as follows:


<script language=JavaScript>

var formdata ='resultsform.asp?IDNumber= <%=objRS(&quot;ID Number&quot;)%> &PartName= <%=objRS(&quot;Part Name&quot;)%> &PartApplication= <%=objRS(&quot;Part Application&quot;)%> &Country= <%=objRS(&quot;Country&quot;)%> &Approver= <%=objRS(&quot;Approver&quot;)%> &Modified= <%=objRS(&quot;Date Last Modified&quot;)%> &Added= <%=objRS(&quot;Date Added&quot;)%> &DocType= <%=objRS(&quot;Document Type&quot;)%>'

function openWindow() {
window.open(formdata,&quot;Results&quot;,&quot;width=250,height=350&quot;);
}
</script>


.......................................

<%
If objRS.Recordcount > 0 then
Response.Write &quot;<table>&quot;
Do until objRS.EOF
Response.Write &quot;<tr>&quot;
Response.Write &quot;<td width='100%'><a href = javascript:eek:penWindow()><font size='2' face='Arial' color='#336699'>&quot; & objRS(&quot;ID Number&quot;) & &quot;     &quot; & objRS(&quot;Part Name&quot;) & &quot;</font></td></a>&quot;
Response.Write &quot;</tr>&quot;
objRS.Movenext
loop

etc. etc.

Any suggestions would be appreciated (and I'm new to this, so I need idiot-proof instructions!!) Thanks!
 
I would make your [tt]openWindow[/tt] function have parameters that will hold the values coming out of your database table. What's happening when you you the [tt]objRS(field)[/tt] in this context is that it's always going to pull the first record from the recordset, since you're not explicitly telling it which row in the recordset to look at. This would make your javascript function look like this:

[tt]
<script language=JavaScript>

var formdata;

function openWindow(id,partName,partApp,country,approver,modDate,addDate,docType) {
formdata = 'resultsform.asp?IDNumber=' + id + '&PartName=' + partName + '&PartApplication=' + partApp + '&Country=' + country + '&Approver=' + approver + '&Modified=' + modDate + '&Added=' + addDate + '&DocType=' + docType;
window.open(formdata,&quot;Results&quot;,&quot;width=250,height=350&quot;);
}
</script>[/tt]

Then, in your VBScript section, where your setting up the links, change it to look like so:

[tt]
<%
Dim strID, strPartName, strPartApp, strCountry
Dim strApprover, strModDate, strAddDate, strDocType
If objRS.Recordcount > 0 then
Response.Write &quot;<table>&quot;
Do until objRS.EOF
If Not IsNull(objRS(&quot;ID Number&quot;)) Then strID = objRS(&quot;ID Number&quot;) else strID = &quot;&quot;
If Not IsNull(objRS(&quot;Part Name&quot;)) Then strPartName = objRS(&quot;Part Name&quot;) else strPartName = &quot;&quot;
If Not IsNull(objRS(&quot;Part Application&quot;)) Then strPartApp = objRS(&quot;Part Application&quot;) else strPartApp = &quot;&quot;
If Not IsNull(objRS(&quot;Country&quot;)) Then strCountry = objRS(&quot;Country&quot;) else strCountry = &quot;&quot;
If Not IsNull(objRS(&quot;Approver&quot;)) Then strApprover = objRS(&quot;Approver&quot;) else strApprover = &quot;&quot;
If Not IsNull(objRS(&quot;Date Last Modified&quot;)) Then strModDate = objRS(&quot;Date Last Modified&quot;) else strModDate = &quot;&quot;
If Not IsNull(objRS(&quot;Date Added&quot;)) Then strAddDate = objRS(&quot;Date Added&quot;) else strAddDate = &quot;&quot;
If Not IsNull(objRS(&quot;Document Type&quot;)) Then strDocType = objRS(&quot;Document Type&quot;) else strDocType = &quot;&quot;
Response.Write &quot;<tr>&quot;
Response.Write &quot;<td width='100%'><a href = javascript:eek:penWindow('&quot; & strID & &quot;','&quot; & strPartName & &quot;','&quot; & strPartApp & &quot;','&quot; & strCountry & &quot;','&quot; & strApprover & ','&quot; & strModDate & &quot;','&quot; & strAddDate & &quot;','&quot; & strDocType & &quot;')><font size='2' face='Arial' color='#336699'>&quot; & objRS(&quot;ID Number&quot;) & &quot; &quot; & objRS(&quot;Part Name&quot;) & &quot;</font></td></a>&quot;
Response.Write &quot;</tr>&quot;
objRS.Movenext
loop

etc. etc.[/tt]

I added local variables to hold the values from the fields in your recordset, for ease of use in constructing the openWindow function call. Then, in the Response.Write section, I added the eight parameters now on the openWindow javascript function in the HTML string construction. Notice that I put single quotes (') around each parameter's value (ex: '&quot; & strID & &quot;') - this is just for consistency's sake, and to get around javascript's tendency to cast any value it can't recognize as an object, which can lead to runtime errors.

Notice, also, that in the javascript I set the value of the formdata variable inside the openWindow function - this means it will change every time that function gets called. Setting it outside the function means that its value never changed, and is probably the biggest reason for your problems.

Hope this was detailed enough for you. Let me know if you need anything else explained.

HTH,
jp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top