Click to See Complete Forum and Search --> : Missing something in my code???
akdiver
09-14-2005, 09:59 PM
Hi
I am new to ASP and im trying to add a search function to a website.
I want the search to return the results chosen but it returns all results. Does anyone see what ive done wrong?
Check it out at
www.make-online-money.net/searcheat.asp
Thanx for your help.
p.s it may be a database thing (keep it in mind)
cheers
russell
09-15-2005, 12:10 AM
uh, yeah, something is wrong, but it'll be pretty hard to guess without some code. Let's see the query you are executing against the db, and tell us what the db provider is (MS SQL, MS Access, MySQL, Oracl etc...). If you are building inline sql in your ASP, then show us the code you are using to build the SQL. Then we can assist. :)
akdiver
09-15-2005, 06:08 PM
<%@Language="Vbscript"%>
<%Option Explicit
Dim con,rs,queries,count,etype,location,delivery,eatin,takeaway,pickup
etype=request.form("etype")
location=request.form("location")
delivery=request.form("delivery")
eatin=request.form("eatin")
takeaway=request.form("takeaway")
pickup=request.form("pickup")%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Criteria</title>
<style type="text/css">
form{margin:0;padding:0}
.hOn{background-color:#EEE}
.hOff{background-color:#FFF}
p{margin:0}
</style>
</head>
<body>
<form id="eat" action="?search=true" method="post">
<div style="width:100%;background-color:#EEE"><strong>Search For Places To Eat</strong></div>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="right">Type: </td>
<td><select name="type" size="1">
<option value="">-- Type --</option>
<option<%if etype="Cafe" then Response.Write(" selected=""selected""")%>>Cafe</option>
<option<%if etype="Restaurant" then Response.Write(" selected=""selected""")%>>Restaurant</option>
<option<%if etype="Take-Away" then Response.Write(" selected=""selected""")%>>Take-Away</option>
<option<%if etype="A-la-Carte" then Response.Write(" selected=""selected""")%>>A-la-Carte</option>
<option<%if etype="Chinese" then Response.Write(" selected=""selected""")%>>Chinese</option>
<option<%if etype="Thai" then Response.Write(" selected=""selected""")%>>Thai</option>
<option<%if etype="Other" then Response.Write(" selected=""selected""")%>>Other</option>
</select>
</td>
</tr>
<tr>
<td> </td><td><input type="submit" value="Search" /></td></tr>
</table>
</form>
<%
if Request.Querystring("search") = "true" then
if etype<>"" AND queries = "" then
queries = "type = '"&etype&"'"
elseif etype<>"" AND queries <> "" then
queries = queries & " AND type = '"&etype&"'"
end if
if queries <> "" then queries = "WHERE "&queries
Set con=Server.CreateObject("ADODB.Connection")
Set rs=Server.CreateObject("ADODB.Recordset")
con.Open"DRIVER={Microsoft Access Driver (*.mdb)};DBQ="&Server.MapPath("eat.mdb")&";password=akdiver"
rs.Open"SELECT * FROM eat "&queries&"",con,3,3
if rs.EOF then%><p>There are no Places To Eat matching your requirements. Please search again for a different Place To Eat.</p><%
end if
Do while not rs.EOF%>
<table width="100%" cellpadding="0" cellspacing="0">
<tr><td colspan="3"> </td></tr>
<tr class="hOff" onmouseout="this.className='hOff'" onmouseover="this.className='hOn'">
<p><strong><%=rs("name")%></strong></p>
<p><%=rs("description")%></p>
<p><%=rs("type")%></p>
</table>
<%rs.MoveNext
Loop
rs.close
con.close
end if%>
<p> </p>
<p style="font-size:smaller">Code by <a href="mailto:dchesterton@gmail.com">Daniel Chesterton</p>
</body>
</html>
russell
09-16-2005, 02:32 AM
Looks like the problem is your element is named "type" and your asp is looking for "etype":
From Your ASP
etype=request.form("etype")
From Your HTML
<select name="type" size="1">
russell
09-16-2005, 02:35 AM
Also, here's a good tip: If you have multiple criteria that you are string building your where clause on, try something like this:
Dim sql
sql = "SELECT * FROM myTable WHERE 1=1"
If len(eType) Then sql = sql & " And etype=" & eType
If len(zzType) Then sql = sql & " And zztype=" & zzType
akdiver
09-16-2005, 03:11 AM
thanx for your help
It works great now
cheers