View Full Version : Help with passing asp values to javascript

09-12-2005, 06:25 PM
I have a main form that is calls (from a button event) the below code to read a database and retreive 4 or 5 fields. I want to return those fields from withing the ASP script back to the form
<META http-equiv="Content-Style-Type" content="text/css">
<script language="JavaScript">
function putLabel(desc){
if (opener && !opener.closed){
<BODY BGCOLOR=#FFFFFF TEXT=#330099 LINK=#CC0000 LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0" basefont size="1" color="green">
<FORM id="Display" name="Display" method="post" action="itemdisplay2.asp">
sub Displayitem ()
If request.form("Item") <> "" then
set fs=Server.CreateObject("Scripting.FileSystemObject")
cfile= "\\\plus\inv/inv/" & request.form("ITEM")
set wfile=fs.openTextFile(cfile,1,FALSE)
do while not wfile.AtEndOfStream
If counter = 3 then
response.write ("Item Desc = " & itemdesc & "<br>")
end If
If counter = 28 then
response.write ("Old Cost = " & oldcost & "<br>")
end If
If counter = 30 then
response.write ("Last Order = " & lastord & "<br>")
end If
If counter = 34 then
response.write ("Tax Code = " & taxcode & "<br>")
end If
If counter = 36 then
response.write ("G.Ledger = " & gledger & "<br>")
end If
set fs=nothing
set wfile=nothing
end if
End Sub
<Input type="text" name="Item" value="" id="item">
<Input type="submit" value="display info">
<!INPUT TYPE='BUTTON' VALUE='Use Values' onClick="javascript:putLabel(<%=itemdesc%>)">
<INPUT TYPE='BUTTON' VALUE='Close Window' onClick="window.close()">


I can't get the asp field value (I'm trying just DESC first) - into the script to return - help anyone?

09-12-2005, 07:53 PM
You have to remember that all the ASP code is processed before the HTML is sent to the browser to display. The way you have your code, you're calling the subroutine for your ASP code AFTER the line where you ask for the variable from your ASP. Also, is this suppose to submit the form to itself? Because you have a Request.Form call in your ASP that refers to an input on the page itself. Request.Form only grabs values from a form that has already been submitted. If the form submits to itself, then that set is ok, except for the subroutine being called after the line where you attempt to use a variable from your ASP code.


09-12-2005, 08:31 PM
Thank for your reply - what I am really trying to do is this

I have a form "createpo" that is collecting a lot of data for a transaction submit. One of the text boxes is for inputting a product ID. Before the form is actually submitted, the user wants to be able to use the value in this text box to open a new window, retreive information from a database (like product name, cost), and populate other text boxes on the original form THEN submit. The best I have been able to do is open a new window where they can SEE the fields - this code was an attempt to return the values to the other form.....

09-12-2005, 09:13 PM
Alright, I don't think I can really help you here. That is much more involved and I'm not advanced enough to really give you the proper help in this without testing the code myself, which I def. don't have time to do, hehe. Good luck and hope someone here can help you.


09-13-2005, 03:36 PM
Does it have to open in a new window? I was thinking that when you loaded the page, you could have some asp code that would write out a JavaScript function with all the pre-filled data stored in arrays. Then when the user typed/selected an id, you could fire off a JavaScript function that would pre-fill the form with the values in the array based on the id typed/selected.


09-13-2005, 06:16 PM
Like this:


' Assign variables
var123 = Request.QueryString("123")
var456 = Request.QueryString("456")

' Output to JavaScript
Response.Write("<SCRIPT type='text/JavaScript'>" & vbcrlf)
Response.Write(" var j123 = '" & var123 & "';" & vbcrlf)
Response.Write(" var j456 = '" & var456 & "';" & vbcrlf)

<BODY OnLoad=something()>
123 = <%= var123 %>,&nbsp 456 = <%= var456 %>

<SCRIPT type='text/JavaScript'>

function something()
alert("123 = " + j123 + "\n" + "456 = " + j456)