cuboidgraphix Posted November 22, 2007 Share Posted November 22, 2007 Hi guys. I have two search forms, one by keyword and another by date range. I also have two different mysql scripts for each form. What I need is an IF script (I think) that will recognize which form I'm using, and run only the script assigned to that form. In more detail.. I want to have both forms at the top. and the results generated at the bottom, but when I search in one form... two different tables are show for both scripts. Can anyone please help me with the script needed? Please and thanks. Quote Link to comment Share on other sites More sharing options...
revraz Posted November 22, 2007 Share Posted November 22, 2007 Post your form code. The form action is what dictates which .php script is called. Quote Link to comment Share on other sites More sharing options...
cuboidgraphix Posted November 22, 2007 Author Share Posted November 22, 2007 Sorry if my code makes no sense what so ever... ??? but I'm a newbie with all of this... and I'm just going by tutorials. <DIV id='mainarea'> <h1>Search</h1> <!-- Form1 Start --> <form name="form1" method="get" action="<?=$PHP_SELF?>"> Seach for: <input type="text" name="word" /> in <select name="field"> <option value=" " selected>Search Field</option> <option value="index2">Index Number</option> <option value="severity">Severity</option> <option value="source">BTS Number</option> <option value="fault_name">Fault Name</option> </select> <input type="submit" name="search1" value="Search" /> </form> <p> <!-- Form2 Start --> <form name="form2" method="get" action="<?=$PHP_SELF?>"> Or from: <Select name="start_year" size="1"> <option value=" " SELECTED>Year</option> <option value="2007">2007</option> </Select> <select name="start_month" size="1"> <option value=" " SELECTED>Month</option> <option value="01">January</option> <option value="02">February</option> <option value="03">March</option> <option value="04">April</option> <option value="05">May</option> <option value="06">June</option> <option value="07">July</option> <option value="08">August</option> <option value="09">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> <select name="start_day" size="1"> <option value=" " selected>Day</option> <option value="01">1</option> <option value="02">2</option> <option value="03">3</option> <option value="04">4</option> <option value="05">5</option> <option value="06">6</option> <option value="07">7</option> <option value="08">8</option> <option value="09">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </select> to: <Select name="end_year" size="1"> <option value=" " selected>Year</option> <option value="2007">2007</option> </Select> <Select name="end_month" size="1"> <option value=" " selected>Month</option> <option value="01">January</option> <option value="02">February</option> <option value="03">March</option> <option value="04">April</option> <option value="05">May</option> <option value="06">June</option> <option value="07">July</option> <option value="08">August</option> <option value="09">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </Select> <Select name="end_day" size="1"> <option value=" " selected>Day</option> <option value="01">1</option> <option value="02">2</option> <option value="03">3</option> <option value="04">4</option> <option value="05">5</option> <option value="06">6</option> <option value="07">7</option> <option value="08">8</option> <option value="09">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </Select> <input type="submit" name="search2" value="Search" /> </form> <!-- Form Script Start --> <?php //----- Script for the first form ----- $word = @$_GET['word']; // define find input variable $trim = trim($word); // define trim whitespace from find variable $field = @$_GET['field']; // define field select variable if ($trim == "") { echo "<p>Please enter a search variable...</p>"; exit; } if ($field == " ") { echo "<p>Your did not select a search field. Please try again!</p>"; exit; } if (!isset($word)) // check for a search parameter { echo "<p>Please enter a search field!</p>"; exit; } mysql_connect("localhost","standby","Standbyguy") or die(mysql_error()); // connect to database mysql_select_db("protech") or die(mysql_error()); // select the database $data = mysql_query("SELECT * FROM alarms WHERE $field like \"%$trim%\" ORDER BY date DESC,time DESC") or die(mysql_error()); $rows = mysql_num_rows($data); if ($rows == 0) { echo "<h4>Search Results</h4>"; echo "<p>Sorry, your search: "" . $trim . "" returned zero results</p>"; } else { print "<h4>Search Results</h4>"; print "<p></p>"; print "<table>"; print "<tr>"; print "<th>Index</th>"; print "<th>Status</th>"; print "<th>Activity</th>"; print "<th>Date</th>"; print "<th>Time</th>"; print "<th>Source</th>"; print "<th>Severity</th>"; print "<th>Fault Name</th>"; print "<th>Sent</th>"; print "</tr>"; } while($info = mysql_fetch_array( $data )) { $src = $info['source']; $col = array(":"); $col_rep = str_replace($col, " ", "$src"); print "<tr>"; print "<td>".$info['index2'] . "</td>"; print "<td>".$info['status'] . "</td>"; print "<td>".$info['activity'] . "</td>"; print "<td>".$info['date'] . "</td>"; print "<td>".$info['time'] . "</td>"; print "<td>".$col_rep . "</td>"; print "<td>".$info['severity'] . "</td>"; print "<td>".$info['fault_name'] . "</td>"; print "<td>".$info['sent'] . " </td>"; print "</tr>"; } Print "</table>"; //----- Script for the second form ----- $sy = @$_GET['start_year']; // start year select variable $sm = @$_GET['start_month']; // start month select variable $sd = @$_GET['start_day']; // start day select variable $ey = @$_GET['end_year']; // end year select variable $em = @$_GET['end_month']; // end month select variable $ed = @$_GET['end_day']; // end day select variable $startdate = $sy."-".$sm."-".$sd; // combination of start variables $enddate = $ey."-".$em."-".$ed; // combination of end variables if ($startdate == " ") { echo "<p>Please enter a start date ...</p>"; exit; } if ($enddate == " ") { echo "<p>Please enter an end date ...</p>"; exit; } mysql_connect("localhost","standby","Standbyguy") or die(mysql_error()); // connect to database mysql_select_db("protech") or die(mysql_error()); // select the database $data = mysql_query("SELECT * FROM alarms WHERE date >= '$startdate' and date <= 'enddate' ORDER BY date DESC,time DESC") or die(mysql_error()); $rows = mysql_num_rows($data); if ($rows == 0) { echo "<h4>Date Range Results</h4>"; echo "<p>Sorry, your search: "" . $trim . "" returned zero results</p>"; } else { print "<h4>Date Range Results</h4>"; print "<p></p>"; print "<table>"; print "<tr>"; print "<th>Index</th>"; print "<th>Status</th>"; print "<th>Activity</th>"; print "<th>Date</th>"; print "<th>Time</th>"; print "<th>Source</th>"; print "<th>Severity</th>"; print "<th>Fault Name</th>"; print "<th>Sent</th>"; print "</tr>"; } while($info = mysql_fetch_array( $data )) { $src = $info['source']; $col = array(":"); $col_rep = str_replace($col, " ", "$src"); print "<tr>"; print "<td>".$info['index2'] . "</td>"; print "<td>".$info['status'] . "</td>"; print "<td>".$info['activity'] . "</td>"; print "<td>".$info['date'] . "</td>"; print "<td>".$info['time'] . "</td>"; print "<td>".$col_rep . "</td>"; print "<td>".$info['severity'] . "</td>"; print "<td>".$info['fault_name'] . "</td>"; print "<td>".$info['sent'] . " </td>"; print "</tr>"; } Print "</table>"; mysql_close() ?> </DIV> Quote Link to comment Share on other sites More sharing options...
revraz Posted November 22, 2007 Share Posted November 22, 2007 You are using PHP_SELF which means it excutes the code in the same page when you submit it. You may have better results if you use a seperate .php file for each action you want to take. Quote Link to comment Share on other sites More sharing options...
cuboidgraphix Posted November 22, 2007 Author Share Posted November 22, 2007 And how can I get it to display on the same page? I want it kind of fancy... where it displays right under both forms. Is that possible? Quote Link to comment Share on other sites More sharing options...
revraz Posted November 22, 2007 Share Posted November 22, 2007 You'll have to reverse your code layout. Instead of having the form up top and php at the bottom, put the php up top and form at the bottom. At the top of the php, you'll need to check if submit1 isset or if submit2 isset. And then execute based on that result. Quote Link to comment Share on other sites More sharing options...
cuboidgraphix Posted November 22, 2007 Author Share Posted November 22, 2007 Is there any way to combine both forms into one and have one search button, but still have the option of searching using the keyword or the date range? Quote Link to comment Share on other sites More sharing options...
revraz Posted November 22, 2007 Share Posted November 22, 2007 You should be able to based on if they entered data in the Date field or in the Keyword field. But what if they enter in both? Quote Link to comment Share on other sites More sharing options...
cuboidgraphix Posted November 22, 2007 Author Share Posted November 22, 2007 hey... I got it to work. Well sorta... my second query yields the whole database.... gotta work on that. maybe I missed something.. but they do work. <DIV id='mainarea'> <h1>Search</h1> <!-- Form Start --> <form name="form1" method="get" action="<?=$PHP_SELF?>"> Seach for: <input type="text" name="word" /> in <select name="field"> <option value=" " selected>Search Field</option> <option value="index2">Index Number</option> <option value="severity">Severity</option> <option value="source">BTS Number</option> <option value="fault_name">Fault Name</option> </select> <input type="submit" name="search1" value="Search" /> </form> <p> <form name="form2" method="get" action="<?=$PHP_SELF?>"> Or from: <Select name="start_year" size="1"> <option value=" " SELECTED>Year</option> <option value="2007">2007</option> </Select> <select name="start_month" size="1"> <option value=" " SELECTED>Month</option> <option value="01">January</option> <option value="02">February</option> <option value="03">March</option> <option value="04">April</option> <option value="05">May</option> <option value="06">June</option> <option value="07">July</option> <option value="08">August</option> <option value="09">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> <select name="start_day" size="1"> <option value=" " selected>Day</option> <option value="01">1</option> <option value="02">2</option> <option value="03">3</option> <option value="04">4</option> <option value="05">5</option> <option value="06">6</option> <option value="07">7</option> <option value="08">8</option> <option value="09">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </select> to: <Select name="end_year" size="1"> <option value=" " selected>Year</option> <option value="2007">2007</option> </Select> <Select name="end_month" size="1"> <option value=" " selected>Month</option> <option value="01">January</option> <option value="02">February</option> <option value="03">March</option> <option value="04">April</option> <option value="05">May</option> <option value="06">June</option> <option value="07">July</option> <option value="08">August</option> <option value="09">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </Select> <Select name="end_day" size="1"> <option value=" " selected>Day</option> <option value="01">1</option> <option value="02">2</option> <option value="03">3</option> <option value="04">4</option> <option value="05">5</option> <option value="06">6</option> <option value="07">7</option> <option value="08">8</option> <option value="09">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </Select> <input type="submit" name="search2" value="Search" /> </form> <!-- Form Script Start --> <?php $search1 =@$_GET['search1']; if (!isset($search1)) { echo " "; } else { $word = @$_GET['word']; // define find input variable $trim = trim($word); // define trim whitespace from find variable $field = @$_GET['field']; // define field select variable if ($trim == "") { echo "<p>Please enter a search variable...</p>"; exit; } if ($field == " ") { echo "<p>You did not select a search field. Please try again!</p>"; exit; } if (!isset($word)) // check for a search parameter { echo "<p>Please enter a search field!</p>"; exit; } mysql_connect("localhost","standby","Standbyguy") or die(mysql_error()); // connect to database mysql_select_db("protech") or die(mysql_error()); // select the database $data = mysql_query("SELECT * FROM alarms WHERE $field like \"%$trim%\" ORDER BY date DESC,time DESC") or die(mysql_error()); $rows = mysql_num_rows($data); if ($rows == 0) { echo "<h4>Search Results</h4>"; echo "<p>Sorry, your search: "" . $trim . "" returned zero results</p>"; } else { print "<h4>Search Results</h4>"; print "<p></p>"; print "<table>"; print "<tr>"; print "<th>Index</th>"; print "<th>Status</th>"; print "<th>Activity</th>"; print "<th>Date</th>"; print "<th>Time</th>"; print "<th>Source</th>"; print "<th>Severity</th>"; print "<th>Fault Name</th>"; print "<th>Sent</th>"; print "</tr>"; } while($info = mysql_fetch_array( $data )) { $src = $info['source']; $col = array(":"); $col_rep = str_replace($col, " ", "$src"); print "<tr>"; print "<td>".$info['index2'] . "</td>"; print "<td>".$info['status'] . "</td>"; print "<td>".$info['activity'] . "</td>"; print "<td>".$info['date'] . "</td>"; print "<td>".$info['time'] . "</td>"; print "<td>".$col_rep . "</td>"; print "<td>".$info['severity'] . "</td>"; print "<td>".$info['fault_name'] . "</td>"; print "<td>".$info['sent'] . " </td>"; print "</tr>"; } Print "</table>"; } $search2 =@$_GET['search2']; if (!isset($search2)) { echo " "; } else { $sy = @$_GET['start_year']; // start year select variable $sm = @$_GET['start_month']; // start month select variable $sd = @$_GET['start_day']; // start day select variable $ey = @$_GET['end_year']; // end year select variable $em = @$_GET['end_month']; // end month select variable $ed = @$_GET['end_day']; // end day select variable $startdate = $sy."-".$sm."-".$sd; // combination of start variables $enddate = $ey."-".$em."-".$ed; // combination of end variables if ($sy == " ") { echo "<p>Please enter a start year ...</p>"; exit; } if ($sm == " ") { echo "<p>Please enter an start month ...</p>"; exit; } if ($sd == " ") { echo "<p>Please enter a start day ...</p>"; exit; } if ($ey == " ") { echo "<p>Please enter a end year ...</p>"; exit; } if ($em == " ") { echo "<p>Please enter an end month ...</p>"; exit; } if ($ed == " ") { echo "<p>Please enter an end day ...</p>"; exit; } mysql_connect("localhost","standby","Standbyguy") or die(mysql_error()); // connect to database mysql_select_db("protech") or die(mysql_error()); // select the database $data = mysql_query("SELECT * FROM alarms WHERE date >= '$startdate' and date <= 'enddate' ORDER BY date DESC,time DESC") or die(mysql_error()); $rows = mysql_num_rows($data); if ($rows == 0) { echo "<h4>Date Range Results</h4>"; echo "<p>Sorry, your search:" . $startdate . "to" . $enddate . "returned zero results</p>"; } else { print "<h4>Date Range Results</h4>"; print "<p></p>"; print "<table>"; print "<tr>"; print "<th>Index</th>"; print "<th>Status</th>"; print "<th>Activity</th>"; print "<th>Date</th>"; print "<th>Time</th>"; print "<th>Source</th>"; print "<th>Severity</th>"; print "<th>Fault Name</th>"; print "<th>Sent</th>"; print "</tr>"; } while($info = mysql_fetch_array( $data )) { $src = $info['source']; $col = array(":"); $col_rep = str_replace($col, " ", "$src"); print "<tr>"; print "<td>".$info['index2'] . "</td>"; print "<td>".$info['status'] . "</td>"; print "<td>".$info['activity'] . "</td>"; print "<td>".$info['date'] . "</td>"; print "<td>".$info['time'] . "</td>"; print "<td>".$col_rep . "</td>"; print "<td>".$info['severity'] . "</td>"; print "<td>".$info['fault_name'] . "</td>"; print "<td>".$info['sent'] . " </td>"; print "</tr>"; } Print "</table>"; } mysql_close() ?> </DIV> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.