Solved

handle a Search with multiple parameters

Posted on 2004-08-21
5
184 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 200 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 300 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

By reading this blog, MSPs will gain insight into how to improve communications with their clients as well as establish a more profitable business.
Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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