Solved

handle a Search with multiple parameters

Posted on 2004-08-21
5
174 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 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

This tutorial shows how to create a greeting card by combining two image layers and a text layer on a PC using a free image editing app.
Employees depend heavily on their PCs, and new threats like ransomware make it even more critical to protect their important data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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