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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you call an asp function from java script

Status
Not open for further replies.

bertrandkis

Programmer
Jul 26, 2002
101
ZA
I need to execute an asp script that calls a stored procedure on SQL server.Each time a form button is clicked the result of the query executed by the asp script must be shown on a form field.With the script I have writen the textbox value stays the same event if the stored procedure returns different values each time it is executed. Here is the code.

<html>
<body>
<script >
function rep()
{
<% SET cn=server.CreateObject(&quot;adodb.connection&quot;)
with cn
.ConnectionString =&quot;Provider=sqloledb;datasource=(local);uid=sa&quot;
.Open
.DefaultDatabase =&quot;Test database&quot;
end with
%>
<%
dt=&quot;3/11/2002&quot;
set rs=server.CreateObject(&quot;adodb.recordset&quot;)
set rs=cn.Execute(&quot;testproc '&quot;& dt &&quot;'&quot; ,,adCmdStoredProc)
response.write(&quot;foo=&quot; & rs.fields(0))
%>
document.f1.t1.value= foo;
}
</script>
<form name=f1>
<input type=text name=t1 >
<input type=button name=b2 value=&quot;New nber&quot; onclick=&quot;rep()&quot;>
</form>
</body>
</html>


The text of the stored procedure is:

create proc testproc @d datetime
as
begin
set dateformat dmy
declare @n int,@k int
set @k=0
set @n=rand()*20+1

while @k=0
begin
if exists(select * from testnumbers where testcode=@n and testdate=@d)
begin
set @n=rand()*20+1
continue
end --end if
else
break
end --end while
select @n --create a recordset
end -- end of proc

The content of the table against which the stored procedure executes:
----------------------------------------
testcode Testdate
----------- ----------------------
7 2002-03-11 00:00:00.000
8 2002-03-11 00:00:00.000
9 2002-03-11 00:00:00.000
10 2002-03-11 00:00:00.000
6 2002-03-11 00:00:00.000


I will appreciate your help and if you can email me directly at bertrandkis@usa.com I will appreciate, the project I am working on has come to a dead end because of this problem. Thank you
 
hey its easy

<input type=text name=t1 value = '<%=rs.fields(0)%>'>
 
Thanks for your reply, I do have that line: <input type=text name=t1 value = '<%=rs.fields(0)%>'> but when you click the button the number in the textbox does not change..Which suggests that a new recordset is not generated whenever the button is clicked.So the problem is still not solved.
 
there are two thing you have to do..
first you have to call a ASP sub
second you have to pass the result to javscript..

here is how to do.

first make a sub
eg.
Sub GetRecord()

'put your searhing code here..
'declare the foo variable

foo= rs.fields(0)

end sub

second make a javascript function
eg.
function ShowTheResult(MyResult)
{
document.f1.t1.value= MyResult;
}

here is the line how to call this both functions..
put this code in the button click..

<input type=button onclick=&quot;<%call GetRecord()%>;ShowTheResult('<%=foo%>')&quot;>

that's all..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top