Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

handle a Search with multiple parameters

Posted on 2004-08-21
5
Medium Priority
?
200 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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Did you know there are services out there that can turn an Instagram feed into an RSS feed? I found some interesting exclusive Instagram content which I wanted to follow without signing up for yet another social media account. RSS to the rescue!
In this article, the configuration steps in Zabbix to monitor devices via SNMP will be discussed with some real examples on Cisco Router/Switch, Catalyst Switch, NAS Synology device.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month12 days, 8 hours left to enroll

971 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