Forum Moderators: open

Message Too Old, No Replies

Go button not working

         

vanjamier

3:17 am on Dec 29, 2004 (gmt 0)

10+ Year Member



Hello.... :(
I dont know whats wrong this time
I cant do a response.write to see results either...
"Go" button kinda not functionining..

<html>
<head>
<title>Update Employee Records</title>
</head>

<body>

<%
'Dimension Local variables
Dim objRS
set oConn=Server.CreateObject("ADODB.Connection")
oConn.Provider="Microsoft.Jet.OLEDB.4.0"
oConn.Open(Server.Mappath("OfficeDB.mdb"))
%>

<form name="form1" method="post" action="SelectEmployee.asp">
Employee ID: <INPUT id=text1 name=text1 size="10">
<input type="submit" name="Submit" value="Go">
<select name="Employee_Id">
<%
strId = Request.Form("text1")

If Request.Form("Submit") = "go" Then

set rs=Server.CreateObject("ADODB.Recordset")

sql="SELECT * FROM Employee WHERE Emp_Id LIKE '%&strId&' "
rs.Open sql,oConn

do until rs.EOF
response.write("<option")

if rs.fields("Emp_Id")= strId then
response.write("selected")
end if

response.write(">")
response.write(rs.fields("Emp_Id"))
rs.MoveNext
loop
rs.Close
set rs=Nothing

End If
%>
</select>
</form>
</body>
</html>

tomasz

4:17 am on Dec 29, 2004 (gmt 0)

10+ Year Member



try if Emp_id varchar type
sql="SELECT * FROM Employee WHERE Emp_Id LIKE '%" & strId & "'"
or if Emp_Id long value
sql="SELECT * FROM Employee WHERE Emp_Id LIKE %" & strId

vanjamier

5:16 am on Dec 29, 2004 (gmt 0)

10+ Year Member



hey i tried both ur suggestions but erm..the button doesn respond.
I did a response.write request.form
and it doesnt return anything

tomasz

2:38 pm on Dec 29, 2004 (gmt 0)

10+ Year Member



try this
If Request.Form("Submit") = "Go" Then

CaseyRyan

3:08 pm on Dec 29, 2004 (gmt 0)

10+ Year Member



Here's what I would change:
*remove the name attribute from the input type=submit tag.
*retrieve just the value of the text1. The code I put in will make sure you have a string to check regardless of whether anything was posted or not.
*check to see if the string is not empty. THis will guard you from retrieving everything in your table as well as check to see if something has been typed in.

...
<form name="form1" method="post" action="SelectEmployee.asp">
Employee ID: <INPUT id=text1 name=text1 size="10">
[b]<input type="submit" value="Go">[/b]
<select name="Employee_Id">
<%
strId = [b]Trim(Request.Form("text1")&"")[/b]
If [b](strId <> "")[/b] Then
...

-=casey=-

mattglet

3:14 pm on Dec 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Probably the most effective way to check for posted data is to check to see if there is anything even in the Form's collection.

Instead of:
If Request.Form("Submit") = "go" Then

Do:
If Request.Form.Count > 0 Then

Zaphod Beeblebrox

11:35 am on Dec 30, 2004 (gmt 0)

10+ Year Member



Also, this line:
if rs.fields("Emp_Id")= strId then
seems to be comparing a numerical value with a string value. If the field in your database is indeed numerical, change the line to:
if CStr(rs.fields("Emp_Id"))= strId then

vanjamier

12:36 am on Dec 31, 2004 (gmt 0)

10+ Year Member



Thanks for all the replies.

Easy_Coder

4:23 pm on Dec 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As a side note consider validating the strId variable on the server side for SQL injection stuff before appending it to your query. Because your doing pass thru sql someone could pass in extra queries to hurt your performance.

So say a user enters this into text1:
' select * from sysusers --

Your query would be adjusted to this:
SELECT * FROM Employee WHERE Emp_Id LIKE '%&' select * from sysusers -- ' strId&'

Or worse what if they pass in an endless loop?

Easy_Coder

4:32 pm on Dec 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



see if this works for you. I was able to access the form submit button value:

<%
For each x in Request.Form
Response.Write x & ": " & Request.Form(x) & "<br>"
Next

Response.Write "<br><br>"

Response.Write "Form Button Value -->" & REquest.Form("go")
%>
<form name="test" method="post">
<input type="text" name="a"><input type="submit" name="go" value="go">
</form>

Zaphod Beeblebrox

7:54 pm on Dec 31, 2004 (gmt 0)

10+ Year Member



I'm sorry, but what do you think any database driver would do with this?

SELECT * FROM Employee WHERE Emp_Id LIKE '%&' select * from sysusers -- ' strId&'

Throw an error...

Security concern is healthy, paranoia isn't.

Easy_Coder

4:26 pm on Jan 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The issue I was pointing out was performance not security although some would argue this as both. That code throws an error only because of syntax. Too, there is no harm/paranoia in suggesting a best practices approach to prevent a user from executing code w/o your knowledge.

sql="SELECT * FROM Employee WHERE Emp_Id LIKE '%&strId&' "

should be

sql="SELECT * FROM Employee WHERE Emp_Id LIKE '% " & strId & " '"

My take is that a user could execute queries w/o your knowledge and therefore impact your performance. I'm not saying that the results of the second query would be displayed. It's obvious that they won't.

This code illustrates that the 2nd query executes when passing in ' select * from sysusers --

strId = Request.Form("text1")
sql="SELECT * FROM Employee WHERE Emp_Id LIKE '% " & strId & " '"

Set oCNN = Server.CreateObject("adodb.connection")
oCNN.Open Application("DB_CONN_STRING")

Set oRST = Server.CreateObject("adodb.recordset")
With oRST
.Open sql, oCNN, adOpenForwardOnly, adLockReadOnly

If Not .EOF Then
aItems = .GetRows()
Else
aItems = Null
End If
End With

Set rs2 = oRST.NextRecordset
aItems2 = rs2.GetRows()

For i = lbound(aItems2,2) To ubound(aItems2,2)
Response.Write (aItems2(0,i)) & "<br>"
Next

vanjamier

1:21 am on Jan 3, 2005 (gmt 0)

10+ Year Member



I was away during the holidays..
Thanks for all the replies.

Zaphod Beeblebrox

8:56 am on Jan 3, 2005 (gmt 0)

10+ Year Member



Easy_Coder: you have a point, but I think the solution is not the correct one. If you'd do it right, you change the line to:

sql="SELECT * FROM Employee WHERE Emp_Id LIKE '%" & Replace(strId, "'", "''") & "'"

This way you won't get an error when someone enters a quote, which is good practice anyway, and it also prevents anything undesired being executed.