Solved

handle a Search with multiple parameters

Posted on 2004-08-21
5
165 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now