Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

handle a Search with multiple parameters

Posted on 2004-08-21
5
Medium Priority
?
197 Views
Last Modified: 2010-04-01
Hello, I have a jsp page with a form containing 6 textboxes where users can enter search parameters. Users should be able to search with one or more parameters entered. Now my question is, how do I handle that in the code? I thought about putting it al in if loops but I cant get it to work properly I'm new to jsp development and I'm pretty stuck at the moment.

what I though of was
if (textbox1.equals("")){ /* do you stuff */ }
if (textbox1.equals("") && textbox2.equals("")){ /* do you stuff */ }
etc etc etc

There has to be an easier way to do this but I really dont know what it is, hope somebody can help me out here, thanks
0
Comment
Question by:babyviper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:Colosseo
ID: 11858499
Hi viper

Here is an example of some code that loops through 6 text fields and if the value is empty displays an alert. I dont think it is exactly what you are after but it is a start.

If you can explain more about what you want for { /* do you stuff */ } then perhaps I can help more, perhaps like building a search sting from the populated fields :)

<html>
<head>
</head>
<script language="JavaScript" type="text/JavaScript">

  function verify_Form() {
    for(i=1;i<=6;i++) {
        if (document.getElementById("text" + i).value == "")
        {
          alert("You must enter a value in field text" + i);
        }
      }
  }

</script>

<body>

<table width="300" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="25%">text 1:</td>
    <td width="75%"><input name="text1" type="text" ></td>
  </tr>
  <tr>
    <td width="25%">text 2:</td>
    <td width="75%"><input name="text2" type="text" ></td>
  </tr>
  <tr>
    <td width="25%">text 3:</td>
    <td width="75%"><input name="text3" type="text" ></td>
  </tr>
  <tr>
    <td width="25%">text 4:</td>
    <td width="75%"><input name="text4" type="text" ></td>
  </tr>
  <tr>
    <td width="25%">text 5:</td>
    <td width="75%"><input name="text5" type="text" ></td>
  </tr>
  <tr>
    <td width="25%">text 6:</td>
    <td width="75%"><input name="text6" type="text" ></td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><input name="botton" type="button" value="Check Page" onClick="verify_Form()"></td>
  </tr>
</table>

</body>
</html>


Regards

Scott
0
 

Author Comment

by:babyviper
ID: 11858533
Hi Scott,

It's not really what I'm after, because the user is not required to fill up all the textfields, I'll try to be more specific.

There are 6 textboxes, if you enter 1 value in 1 of the textboxes a sql query is set and executed, each textbox triggers a seperate query. I do a check on the query to see if I get some rows back from the query and show them on the page, if no rows are returned I show an error. It All works fine if people enter parameters in only 1 textfield but here comes my problem, when people enter parameters in more then 1 textbox the query needs to be different to be able to receive the exact amount of rows. I dont know how to handle that, I mean how to get the exact returned rows when people enter more then 1 textfield. When I get back home I'll post the code I have at the moment, the part do you stuff just sets the query executes it.

Regards,

Jean
0
 
LVL 15

Assisted Solution

by:Colosseo
Colosseo earned 800 total points
ID: 11858620
Hi Jean

I think i understand. this code might be closer to what you want, but we can tidy it up when you post your code

<html>
<head>
</head>
<script language="JavaScript" type="text/JavaScript">

  function verify_Form() {
    var searchSelect = "SELECT [field1], [field2] FROM [table1]"
    var searchString = ""
    for(i=1;i<=6;i++) {
        if (document.getElementById("text" + i).value != "")
        {
          if (searchString == "") {
             searchString = " WHERE " + document.getElementById("text" + i).dbField + " = '" + document.getElementById("text" + i).value + "'"
            } else {
              searchString = searchString + " AND " + document.getElementById("text" + i).dbField + " = '" + document.getElementById("text" + i).value + "'"
            }
        }
      }
      alert(searchSelect + searchString);
  }

</script>

<body>

<table width="300" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="25%">text 1:</td>
    <td width="75%"><input name="text1" dbField="fieldname1" type="text" ></td>
  </tr>
  <tr>
    <td width="25%">text 2:</td>
    <td width="75%"><input name="text2" dbField="fieldname2" type="text" ></td>
  </tr>
  <tr>
    <td width="25%">text 3:</td>
    <td width="75%"><input name="text3" dbField="fieldname3" type="text" ></td>
  </tr>
  <tr>
    <td width="25%">text 4:</td>
    <td width="75%"><input name="text4" dbField="fieldname4" type="text" ></td>
  </tr>
  <tr>
    <td width="25%">text 5:</td>
    <td width="75%"><input name="text5" dbField="fieldname5" type="text" ></td>
  </tr>
  <tr>
    <td width="25%">text 6:</td>
    <td width="75%"><input name="text6" dbField="fieldname6" type="text" ></td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><input name="botton" type="button" value="Check Page" onClick="verify_Form()"></td>
  </tr>
</table>

</body>
</html>

Regards

Scott
0
 
LVL 14

Accepted Solution

by:
boonleng earned 1200 total points
ID: 11858865
Is this something you looking for?

List criteria = new ArrayList();
if (textbox1 != null && textbox1.length() > 0) {
  criteria.add("field1 = "  + textbox1);
}
if (textbox2 != null && textbox2.length() > 0) {
  criteria.add("field2 = "  + textbox2);
}
//the rest of the parameters

if (!criteria.isEmpty) {
  StringBuffer sql = new StringBuffer();
  sql.append("SELECT * FROM tableA WHERE ");
  for (int i=0; i<criteria.size(); i++) {
    sql.append(criteria.get(i));
    if (i < criteria.size() - 1) {
      sql.append(" AND ");
    }
  }

  //execute the sql
}
0
 

Author Comment

by:babyviper
ID: 11863713
Thanks allot guys I got it to work thanks to the both of you !!!!!!!!! You guys really helped me out here, especially boonleng with the jsp code. I decided to split the points between you guys, again THANKS ALLOT FOR YOUR HELP !!!!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Want to know how to use Exchange Server Eseutil command? Go through this article as it gives you the know-how.
What we learned in Webroot's webinar on multi-vector protection.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question