Hello,
I'm using a form to query a Mysql database. This form contains 1 group of radio buttons called R1. The code for the form(form1) & the form processor is below. Everything is working fine.

Now, I want to add another 2 groups of radio buttons to my form called radiobutton1 and radiobutton2. The code for the new form(form2) is below. My problem is I don't know how to write the code for the form processor. I believe that I must change the switch in the form processor to an array in order to simplify the code. I don't know how to write the array.

Additional info - the databases cannot be consolidated into 1 database with many tables because they(the databases) are very big. My webhost does not allow any individual database to exceed 50 MB.

Thanks a lot for your help.

Code for Form1:-

<form name="form1" method="POST" action="">
 <tr>
   <td><div align="center">
     <input type="radio" value="kl06" name="R1">
     Kuala Lumpur 2006<strong> or
     <input name="R1" type="radio" value="sl06">
     </strong>Selangor 2006<strong> or
     <input name="R1" type="radio" value="kl07">
     </strong>Kuala Lumpur 2007<strong> or
     <input name="R1" type="radio" value="sl07" checked>
     </strong>Selangor 2007 </div></td>
 </tr>
 <tr>
   <td><div align="center">Key in Your Search Criteria Below </div></td>
 </tr>
 <tr>
   <td><div align="center">
     <input type="text" name="Text_Box_1" size="20" tabindex="6">
     <strong>or</strong>
     <input type="text" name="Text_Box_2" size="20" tabindex="7">
     <strong>or</strong>
     <input type="text" name="Text_Box_3" size="20" tabindex="8">
     <strong>or</strong>
     <input type="text" name="Text_Box_4" size="20" tabindex="9">
   </div></td>
 </tr>
 <tr>
   <td><div align="center">
     <input type="text" name="Text_Box_5" size="20" tabindex="10">
     <strong>or</strong>
     <input type="text" name="Text_Box_6" size="20" tabindex="11">
     <strong>or</strong>
     <input type="text" name="Text_Box_7" size="20" tabindex="12">
     <strong>or</strong>
     <input type="text" name="Text_Box_8" size="20" tabindex="13">
   </div></td>
 </tr>
 <tr>
   <td><div align="center">
     <input name="submit" type="submit" id="submit2" value="Submit" tabindex="14">
     <input name="reset2" type="reset" id="reset2" value="Reset" tabindex="15">
   </div></td>
 </tr>
</form>

Code for Form Processor:-

switch($_POST['R1'])
{
case 'sl06':
$username="something1";
$password="samestuff";
$database="database1";
$host="localhost";
mysql_connect ("$host","$username","$password");
mysql_select_db($database) or die( "Where's the database man?");
$query="SELECT * FROM selangor WHERE (address like '%$Text_Box_1%') OR (address like '%$Text_Box_2%') OR (address like '%$Text_Box_3%') OR (address like '%$Text_Box_4%') OR (address like '%$Text_Box_5%') OR (address like '%$Text_Box_6%') OR (address like '%$Text_Box_7%') OR (address like '%$Text_Box_8%') OR (name like '%$Text_Box_1%') OR (name like '%$Text_Box_2%') OR (name like '%$Text_Box_3%') OR (name like '%$Text_Box_4%') OR (name like '%$Text_Box_5%') OR (name like '%$Text_Box_6%') OR (name like '%$Text_Box_7%') OR (name like '%$Text_Box_8%') OR (phone like '%$Text_Box_1%') OR (phone like '%$Text_Box_2%') OR (phone like '%$Text_Box_3%') OR (phone like '%$Text_Box_4%') OR (phone like '%$Text_Box_5%') OR (phone like '%$Text_Box_6%') OR (phone like '%$Text_Box_7%') OR (phone like '%$Text_Box_8%') LIMIT 0, 15";
break;
case 'kl06':
$username="something2";
$password="samestuff";
$database="database2";
$host="localhost";
mysql_connect ("$host","$username","$password");
mysql_select_db($database) or die( "Where's the database man?");
$query="SELECT * FROM wilayah WHERE (address like '%$Text_Box_1%') OR (address like '%$Text_Box_2%') OR (address like '%$Text_Box_3%') OR (address like '%$Text_Box_4%') OR (address like '%$Text_Box_5%') OR (address like '%$Text_Box_6%') OR (address like '%$Text_Box_7%') OR (address like '%$Text_Box_8%') OR (name like '%$Text_Box_1%') OR (name like '%$Text_Box_2%') OR (name like '%$Text_Box_3%') OR (name like '%$Text_Box_4%') OR (name like '%$Text_Box_5%') OR (name like '%$Text_Box_6%') OR (name like '%$Text_Box_7%') OR (name like '%$Text_Box_8%') OR (phone like '%$Text_Box_1%') OR (phone like '%$Text_Box_2%') OR (phone like '%$Text_Box_3%') OR (phone like '%$Text_Box_4%') OR (phone like '%$Text_Box_5%') OR (phone like '%$Text_Box_6%') OR (phone like '%$Text_Box_7%') OR (phone like '%$Text_Box_8%') LIMIT 0, 15";
break;
case 'sl07':
$username="something3";
$password="samestuff";
$database="database3";
$host="localhost";
mysql_connect ("$host","$username","$password");
mysql_select_db($database) or die( "Where's the database man?");
$query="SELECT * FROM selangor WHERE (address like '%$Text_Box_1%') OR (address like '%$Text_Box_2%') OR (address like '%$Text_Box_3%') OR (address like '%$Text_Box_4%') OR (address like '%$Text_Box_5%') OR (address like '%$Text_Box_6%') OR (address like '%$Text_Box_7%') OR (address like '%$Text_Box_8%') OR (name like '%$Text_Box_1%') OR (name like '%$Text_Box_2%') OR (name like '%$Text_Box_3%') OR (name like '%$Text_Box_4%') OR (name like '%$Text_Box_5%') OR (name like '%$Text_Box_6%') OR (name like '%$Text_Box_7%') OR (name like '%$Text_Box_8%') OR (phone like '%$Text_Box_1%') OR (phone like '%$Text_Box_2%') OR (phone like '%$Text_Box_3%') OR (phone like '%$Text_Box_4%') OR (phone like '%$Text_Box_5%') OR (phone like '%$Text_Box_6%') OR (phone like '%$Text_Box_7%') OR (phone like '%$Text_Box_8%') LIMIT 0, 15";
break;
case 'kl07':
$username="something4";
$password="samestuff";
$database="database4";
$host="localhost";
mysql_connect ("$host","$username","$password");
mysql_select_db($database) or die( "Where's the database man?");
$query="SELECT * FROM wilayah WHERE (address like '%$Text_Box_1%') OR (address like '%$Text_Box_2%') OR (address like '%$Text_Box_3%') OR (address like '%$Text_Box_4%') OR (address like '%$Text_Box_5%') OR (address like '%$Text_Box_6%') OR (address like '%$Text_Box_7%') OR (address like '%$Text_Box_8%') OR (name like '%$Text_Box_1%') OR (name like '%$Text_Box_2%') OR (name like '%$Text_Box_3%') OR (name like '%$Text_Box_4%') OR (name like '%$Text_Box_5%') OR (name like '%$Text_Box_6%') OR (name like '%$Text_Box_7%') OR (name like '%$Text_Box_8%') OR (phone like '%$Text_Box_1%') OR (phone like '%$Text_Box_2%') OR (phone like '%$Text_Box_3%') OR (phone like '%$Text_Box_4%') OR (phone like '%$Text_Box_5%') OR (phone like '%$Text_Box_6%') OR (phone like '%$Text_Box_7%') OR (phone like '%$Text_Box_8%') LIMIT 0, 15";
}

Code for Form2:-

<form name="form2" method="post" action="">
 <tr>
   <td><div align="center">
     <input type="radio" value="kl06" name="R1">
Kuala Lumpur 2006<strong> or
<input name="R1" type="radio" value="sl06">
</strong>Selangor 2006<strong> or
<input name="R1" type="radio" value="kl07">
</strong>Kuala Lumpur 2007<strong> or
<input name="R1" type="radio" value="sl07" checked>
</strong>Selangor 2007</div></td>
 </tr>
 <tr>
   <td><div align="center">Key in Your Search Criteria Below</div></td>
 </tr>
 <tr>
   <td><div align="center">Search by
       <input name="Text_Box_1" type="text" id="Text_Box_1" tabindex="6" value="Name" size="15">
       <label>
       <input name="radiobutton1" type="radio" value="and">
       <strong>and</strong></label>
       <input name="radiobutton1" type="radio" value="or" checked>
       <strong>or </strong>
       <input name="Text_Box_2" type="text" id="Text_Box_2" tabindex="7" value="Phone" size="15">
       <label>
       <input name="radiobutton2" type="radio" value="and">
       <strong>and</strong></label>
       <input name="radiobutton2" type="radio" value="or" checked>
       <strong>or </strong>
       <input name="Text_Box_3" type="text" id="Text_Box_3" tabindex="8" value="Address" size="15">
</div></td>
 </tr>
 <tr>
   <td><div align="center">
     <input name="submit" type="submit" id="submit" value="Submit" tabindex="14">
     <input name="reset22" type="reset" id="reset22" value="Reset" tabindex="15">
</div></td>
 </tr>
</form>
    6 days later

    Each radio group is one $POST[] var, with value according to the value of the button in the group that is selected. I think that if nothing is selected, the group will never be $POSTed so you must check with isset() for existence.

    That way, if I am understanding your problem correctly, if you want three groups to act exclusively, you can nest switches. In default: of first switch (for first group) you nest switch for second group, and in default of second switch you nest third. OR if that is cumbersome, call functions that process other two groups (postvars).

      Thanks for your reply, void_function. Yes, you have understood my issue correctly. I would be grateful if you could show me the code.

      Thanks.

        Like this?

        
        $host="localhost";
        
        switch($_POST['R1'])
        {
        case 'sl06':
          $username="something1";
          $password="samestuff";
          $database="database1";
          $tablename= "selangor";
          $year= '2006';
          break;
        
        case 'kl06':
          $username="something2";
          $password="samestuff";
          $database="database2";
          $tablename= 'wilayah';
          $year= '2006';
          break;
        
        case 'sl07':
          $username="something3";
          $password="samestuff";
          $database="database3";
          $tablename= 'selangor';
          $year= '2007';
          break;
        
        case 'kl07':
          $username="something4";
          $password="samestuff";
          $database="database4";
          $tablename= 'wilayah';
          $year= '2007';
          break;
        
        default:
          switch ($_POST['R2']) {
            // repeat here code for R2;
          default:
            switch ($_POST['R3']) {
              // Code for R3, etc...    
        } } } $query= "SELECT * FROM $tablename WHERE (address like '%$Text_Box_1%') OR (address like '%$Text_Box_2%') OR (address like '%$Text_Box_3%') OR (address like '%$Text_Box_4%') OR (address like '%$Text_Box_5%') OR (address like '%$Text_Box_6%') OR (address like '%$Text_Box_7%') OR (address like '%$Text_Box_8%') OR (name like '%$Text_Box_1%') OR (name like '%$Text_Box_2%') OR (name like '%$Text_Box_3%') OR (name like '%$Text_Box_4%') OR (name like '%$Text_Box_5%') OR (name like '%$Text_Box_6%') OR (name like '%$Text_Box_7%') OR (name like '%$Text_Box_8%') OR (phone like '%$Text_Box_1%') OR (phone like '%$Text_Box_2%') OR (phone like '%$Text_Box_3%') OR (phone like '%$Text_Box_4%') OR (phone like '%$Text_Box_5%') OR (phone like '%$Text_Box_6%') OR (phone like '%$Text_Box_7%') OR (phone like '%$Text_Box_8%') LIMIT 0, 15"; $db= mysql_connect ($host, $username, $password); mysql_select_db ($database, $db); if ($db) $res= mysql_query($query) or die($query_error_string); else echo $connect_error_string;

          No, not as simple as that.

          ($_POST['R1']) is a group of radio buttons that determine which database the form is supposed to query.

          ($_POST['radiobutton1']) is a group of radio buttons which determine the operator AND & OR between Text_Box_1 & Text_Box_2.

          ($_POST['radiobutton2']) is a group of radio buttons which determine the operator AND & OR between Text_Box_2 & Text_Box_3.

          So the flow of events should look like this:-
          1. First, the form connects to the right database in ($POST['R1']).
          2. Second, depending on whether the user selected case AND or case OR in ($
          POST['radiobutton1']), the query should look something like this

          case 'AND':
          $query= "SELECT * FROM $tablename WHERE (address like '%$Text_Box_1%') AND (address like '%$Text_Box_2%');
          
          case 'OR':
          $query= "SELECT * FROM $tablename WHERE (address like '%$Text_Box_1%') OR (address like '%$Text_Box_2%');

          3. Step number 2 is repeated here to compare Text_Box_2 and Text_Box_3 based on what was selected in ($_POST['radiobutton2']).

            Well, then it appears you've solved your problem. Just, in stead of repeating entire query string in different cases, assign vars:

            $whereOperator12='OR'; // for situation OR
            $whereOperator12='AND'; // for situation AND

            You can also use $whereOperator23 for comparing textbox 2 and 3 in separate switch (or "if" clause if you have only two possible values).

            $username, $password, $database and $tablename depend on the first radio group value. $host is the same for all cases. And then construct the query:

            SELECT * FROM $tablename WHERE (address like '%$Text_Box_1%') $whereOperator12 (address like '%$Text_Box_2%') ...

              Write a Reply...