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

Passing form variable to JavaScript/ASP function

Status
Not open for further replies.

BradEdwards

Technical User
Oct 7, 2000
25
0
0
US
I have a form, let's say for simplicity sake, with 2 input fields

<input type=&quot;text&quot; name=&quot;empNum&quot; onchange=&quot;empName.value=GetUserName(this.value)&quot;>
<input type=&quot;text&quot; name=&quot;empName&quot;>

Now i have a db that stores all the employee information which is linked to an employee number. What i want to do is when the employee types in their employee number the GetUserName() function accesses the database and returns the Name of the employee.

Here is my function:

<script>
function GetUserName(objEmpNum)
{
<%
Dim rs, strSQL

strSQL = &quot;Select (strFirstName + ' ' + strLastName) AS FullName, intEmpNum from tblEmployees WHERE intEmpNum=79&quot;
set rs=Server.CreateObject(&quot;ADODB.Recordset&quot;)
objconn.execute &quot;Use dbGeneral&quot;
rs.open strSQL, objconn
%>
return &quot;<% =rs(&quot;FullName&quot;) %>&quot;;
}
</script>

Right now it works, but as you can see I'm selecting the data based on the EmpNum. So right now any number I put in the EmpNum field on the form it comes back with the name of employee number 79. What I would like to know how to do is create a select statement based on the EmpNum value passed through from the form. From what i've read since ASP is server side and Javascript is client side, I can't send a JavaScript variable to ASP code. What other way can I use to create my select statement?? Thanks.
 
Hi Brad

If I were you, use the form and have it submitted to either the same page or to another page (this gets the variable into the server)

By calling

intEmpNum = Request.Form(&quot;empNum&quot;)

Then run this into your sql statement as before.

Check out asp101.com for some good examples of this

HTH

Tim
 
Hey Tim,

Here is my code (simplified). I just can't seem to figure it out. If you see something I don't please let me know. I dont't think it's pulling the value from the form. Thanks.

filename= test.asp

<html>
<head>
<TITLE>Employee Name Test</TITLE>
</head>
<body>
<br>

<Script language=&quot;JavaScript&quot;>
<!-- Hide code from other browsers

function GetUserName()
{
<%
Dim rs, strSQL, strConnect, objConn
Dim EmpNum

EmpNum = Request.Form(&quot;EmpNum&quot;)
strConnect = &quot;Provider=sqloledb;Data Source=ISERV1_NT;Initial Catalog=dbGeneral;User Id=web_usr;Password=web&quot;

strSQL = &quot;Select (strFirstName + ' ' + strLastName) AS FullName, intEmpNum from tblEmployees WHERE intEmpNum=&quot; & EmpNum
Set objConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
set rs=Server.CreateObject(&quot;ADODB.Recordset&quot;)
objConn.open strConnect

rs.open strSQL, objconn
%>
return(&quot;<% =rs(&quot;FullName&quot;) %>&quot;);
}
// End of code -->
</Script>

<form method=&quot;post&quot;>
<input type=&quot;text&quot; name=&quot;EmpNum&quot; onchange=&quot;EmpName.value=GetUserName();&quot;>
<input type=&quot;text&quot; name=&quot;EmpName&quot;>

</form>
</body>
</html>
 
Tim,

I think I figured out my problem but I still don't have a solution. I'm not POSTing the form data. The page loads with the input boxes and then I start entering information. The data never gets posted so using the Request.Form method probably won't work for me right?? Anyway hope that helps. Is there any other way I can do this??
 
Maybe this is oversimplified, or I don't really understand the problem, but couldn't you use an &quot;action&quot; parameter in the initial form tag, calling the same page? Then when the page is submitted using a submit button or whatever, the form variables would be posted. Is there some reason you can't post the variables? I think that does the same thig as timferris suggested, just using html instead.
 
Here's what's going on...I have a page that loads and creates the following input boxes:

Date
Employee Number
Employee Name
Customer Name
Product
Referral

Add button

The employee would enter their Employee Number and I would like the Employee Name field to populate automatically based on the Employee Number. This would happen before I even press the Add (Submit) button. So the POST won't work in my case because I want the field to update before I POST.
 
Try this :

<form name=&quot;main&quot; action=&quot;post&quot;>

<input type=&quot;text&quot; name=&quot;empnum&quot; value=&quot;<%= Request(&quot;empnum&quot;) %>&quot; onBlur=&quot;main.submit();return false;&quot;>

[...]

The moment you tab out of the empnum input box the form will be submitted to itself. You could program the preceding asp code to assume that if the only info submitted was empnum, it would query the db for the rest of the emp data & fill in the form accordingly.



 
i want the user to type in the username and passowrd, on verfication open the respect page if validation is true ,else type the msg &quot;try again&quot;.

i wnat to know the answer for the first qoestion
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top