PHP search multiple input field box help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
julzk
Forum Newbie
Posts: 23
Joined: Fri Oct 30, 2009 4:42 am

PHP search multiple input field box help

Post by julzk »

I am having a problem with my search script. At current it will simply search by a selected date which is a java based calendar that displays when using id='datedisplay' in the input form field below. What I want it to do is have an extra input text field box that can search for text/words within a table field. So my query would be something like

Code: Select all

SELECT * FROM tbl_comments WHERE comments_date LIKE '%$search%' OR comments_content LIKE '%$textsearch%' ORDER BY comments_date DESC, comments_time DESC
But now it gets tricky. I want it to work so if someone types something into the text input box, it will ignore the other input box that searches by date. And if the text input search box is empty, it will ignore the text input box and search only by the date input box.

I am assuming it's going to involve some if statements and posssible !empty and }else{ commands, I am just not 100% sure how to piece what I want together within my search code below.

Here is my form code:

Code: Select all

 
     echo "<form method='post' action='handover_index.php'>";
     echo "<input type='text' name='search' id='datedisplay' style='width: 60px;' maxlength='10'> ";
     echo "<input type='hidden' name='cmd' value='search' />";
     echo " <input type='Submit' name='submit' value='Go!'>";
     echo "</form>";
 
And here is my php code:

Code: Select all

 
// ##### Search DB ##### Start ->
$str = str_replace("\n", "<BR />", $str);
if (strlen($_POST["search"]) > 1 or strlen($_GET["search"]) > 1)
{
if(isset($_POST["search"])) $search=$_POST["search"];
if(isset($_GET["search"])) $search=$_GET["search"];
 
$result = mysql_query("SELECT * FROM tbl_comments WHERE comments_date LIKE '%$search%' ORDER BY comments_date DESC, comments_time DESC");
 
echo "<table width='98%' cellspacing='0' cellpadding='3' border='0'>";
 
$i = 0;
while($r=mysql_fetch_array($result))
{   
      $comments_id=$r["comments_id"];
     $comments_date=$r["comments_date"];
     $comments_time=$r["comments_time"];
     $comments_user=$r["comments_user"];
      $comments_content=str_replace("\r\n","<br>",$r[comments_content]);
      $comments_updateuser=$r["comments_updateuser"];
      $comments_updatedate=$r["comments_updatedate"];
     $comments_updatetime=$r["comments_updatetime"];
    
     list($thour, $tmin, $tsec) = explode(':', $comments_time);
     list($uthour, $utmin, $utsec) = explode(':', $comments_updatetime);
    
      if ($_GET[cmd]== "edit" and $_GET[comments_id] == $comments_id) {
    
     $comments_content=str_replace("<br>","\r\n",$r[comments_content]);
      echo "<tr>";
      echo "<form action='handover_index.php' method='post'>";
     echo "<td class='rowtxt' valign='top' width='375'><font class='rowadded'>Added</font> <font class='rowaddeddata'>$comments_date</font> <font class='rowadded'>at</font> <font class='rowaddeddata'>$comments_time</font> <font class='rowadded'>by</font> $comments_user</td>";
     echo "<td class='rowtxt' valign='top' width='45'><div class='rowalign'><input type=submit name=submit value=Update><input type=hidden name=comments_id value=$comments_id></div></td>";
     echo "<tr></tr>";
     echo "<td class='rowtxt' valign='top' colspan='2' width='100%'><textarea name=comments_content cols='10' rows='5'>$comments_content</textarea><br><br></td>";
     echo "</form>";
     echo "</tr>";
   
      }else{ if($i%2 == 0){
 
       echo "<tr class='rowresult1'>";
      echo "<td class='txt' valign='top' width='375'> <font class='rowadded'>Added</font> <font class='rowaddeddata'>$comments_date $thour:$tmin</font> - $comments_user";
      if (!empty($comments_updateuser)) {
         echo "<font size='2' color='#000000'> / </font> <font class='rowadded'> Updated</font> <font class='rowaddeddata'>$comments_updatedate $uthour:$utmin</font> - " .$comments_updateuser. "";
      }
      echo "</td>";
      echo "<td class='rowtxt' valign='top' width='45'><div class='rowalign'><INPUT TYPE='button' name='edit' value='Edit' onClick=self.location='handover_index.php?cmd=edit&comments_id=$comments_id&search=$search'></div></td>";
      echo "</tr><tr class='rowresult1'>";
      echo "<td class='rowtxt' colspan='2' valign='top' width='100%'>$comments_content<br><br>";
      echo "</td></tr>";
      $i++;
      
      }else{
      
       echo "<tr class='rowresult2'>";
      echo "<td class='txt' valign='top' width='375'> <font class='rowadded'>Added</font> <font class='rowaddeddata'>$comments_date $thour:$tmin</font> - $comments_user";
      if (!empty($comments_updateuser)) {
         echo "<font size='2' color='#000000'> / </font> <font class='rowadded'> Updated</font> <font class='rowaddeddata'>$comments_updatedate $uthour:$utmin</font> - " .$comments_updateuser. "";
      }
      echo "</td>";
      echo "<td class='rowtxt' valign='top' width='45'><div class='rowalign'><INPUT TYPE='button' name='edit' value='Edit' onClick=self.location='handover_index.php?cmd=edit&comments_id=$comments_id&search=$search'></div></td>";
      echo "</tr><tr class='rowresult2'>";
      echo "<td class='rowtxt' colspan='2' valign='top' width='100%'>$comments_content<br><br>";
      echo "</td></tr>";
      $i++;
        }
     }
    }
echo "</table>";
echo "<br>";
}else{
// ##### Search DB ##### End <-
 
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: PHP search multiple input field box help

Post by cpetercarter »

A simple answer is to use two forms - one for the date and one for the text.

Otherwise, something like this:

Code: Select all

if (!empty($_POST['search_text'])) {
//search by text
}
elseif (!empty($_POST['search_date']))  {
//search by date
}
 
This will search by text if the user put something in the text box; and by date if there is something in the date box and nothing in the text box.

Or, if you prefer:

Code: Select all

 
if (!empty($_POST['search_text'])) { //if there is something in search_text
     if (empty($_POST['search_date']))  {
          //search by text
       } else {
          //combined text and date search
        }
} else  { //if there is nothing in search_text
       if (!empty($_POST['search_date']))  {
          // search by date
         }  else  {
          // error - no data in either $_POST['search_text'] or $_POST['search_date']
         }
}
 
julzk
Forum Newbie
Posts: 23
Joined: Fri Oct 30, 2009 4:42 am

Re: PHP search multiple input field box help

Post by julzk »

The bottom suggestion you put, will that work with my search?

When I look at the Search form, there are two input boxes like so:

[_________] [30-10-2009] [Go!]

I have a calendar that always has the current date pre-filled in via a java calendar. If someone simply selects a different date leaving the first input text field empty, it will simply run my SQL query and display results by date only. However, if someone was to enter a word or a few words into the first empty input field box on the left and run a search, it will search purely by what's in the input text form field and disregard whatever is in the second input form field which contains the date.
arnaud
Forum Newbie
Posts: 3
Joined: Thu Oct 22, 2009 10:38 am

Re: PHP search multiple input field box help

Post by arnaud »

try this ...

Code: Select all

 
<?php
# // ##### Search DB ##### Start ->
$str = str_replace("\n", "<BR />", $str);
if (strlen($_POST["search"]) > 1 or strlen($_GET["search"]) > 1)
{
    if(isset($_POST["search"])) $search=$_POST["search"];
    if(isset($_GET["search"])) $search=$_GET["search"];
 
// if someone types something into the text input box,
// it will ignore the other input box that searches by date.
// And if the text input search box is empty, it will ignore the text
// input box and search only by the date input box
 
    $query = "SELECT * FROM tbl_comments ";
 
    if(isset($_POST["textsearch"])) {
        $query .= "WHERE comments_content LIKE '%$textsearch%'
                  ORDER BY comments_date DESC, comments_time DESC";
    } else {
        $query .= "WHERE comments_date LIKE '%$search%'
                  ORDER BY comments_date DESC, comments_time DESC";
    }
 
    $result = mysql_query($query)
;

this code is not very well designed I see many problems:

1 - you must ALWAYS escape user input
2 - why test input from $_POST and $_GET ???
3 - you must not use "SELECT * "
4 - you MUST separate view layer from business layer
...
julzk
Forum Newbie
Posts: 23
Joined: Fri Oct 30, 2009 4:42 am

Re: PHP search multiple input field box help

Post by julzk »

In your query, you used

Code: Select all

WHERE comments_date LIKE '%$search%'
and

Code: Select all

WHERE comments_content LIKE '%$textsearch%'
Does this mean I would need two forms with two separate form and submit buttons? I want to try and do it so I have only one form like this:

[_________] [30-10-2009] [Go!]
arnaud
Forum Newbie
Posts: 3
Joined: Thu Oct 22, 2009 10:38 am

Re: PHP search multiple input field box help

Post by arnaud »

no, you just add the input text field to your initial form. Something like:

Code: Select all

 
<form method='post' action='handover_index.php'>
<input type='text' name='search' id='datedisplay' style='width: 60px;' maxlength='10'>
<input type='text' name='textsearch' id='textsearch' style='width: 60px;'>
<input type='hidden' name='cmd' value='search' />
<input type='Submit' name='submit' value='Go!'>
</form>
 
julzk
Forum Newbie
Posts: 23
Joined: Fri Oct 30, 2009 4:42 am

Re: PHP search multiple input field box help

Post by julzk »

Hmm, does not work. When I attempt to search by text, it's just spitting out everything stored within the database. And when I leave the text input field blank, it's spitting out the same result again when searching by date.

Here's the code for my form:

Code: Select all

      echo "<form method='post' action='handover_index.php'>";
      echo "<input type='text' name='textsearch' id='textsearch' style='width: 60px;' maxlength='10'>";
      echo "<input type='text' name='search' id='datedisplay' style='width: 60px;' maxlength='10'> ";
      echo "<input type='hidden' name='cmd' value='search' />";
      echo " <input type='Submit' name='submit' value='Go!'>";
      echo "</form>";
And here is the php code:

Code: Select all

// ##### Search DB ##### Start ->
$str = str_replace("\n", "<BR />", $str);
if (strlen($_POST["search"]) > 1 or strlen($_GET["search"]) > 1)
{
if(isset($_POST["search"])) $search=$_POST["search"];
if(isset($_GET["search"])) $search=$_GET["search"];
  
     $query = "SELECT * FROM tbl_comments ";
     
     if(isset($_POST["textsearch"])) {
         $query .= "WHERE comments_content LIKE '%$textsearch%'
                   ORDER BY comments_date DESC, comments_time DESC";
     } else {
         $query = "WHERE comments_date LIKE '%$search%'
                   ORDER BY comments_date DESC, comments_time DESC";
     }
     $result = mysql_query($query);
 
echo "<table width='98%' cellspacing='0' cellpadding='3' border='0'>";
 
$i = 0;
while($r=mysql_fetch_array($result))
{   
      $comments_id=$r["comments_id"];
      $comments_date=$r["comments_date"];
      $comments_time=$r["comments_time"];
      $comments_user=$r["comments_user"];
      $comments_content=str_replace("\r\n","<br>",$r[comments_content]);
      $comments_updateuser=$r["comments_updateuser"];
      $comments_updatedate=$r["comments_updatedate"];
      $comments_updatetime=$r["comments_updatetime"];
      
      list($thour, $tmin, $tsec) = explode(':', $comments_time);
      list($uthour, $utmin, $utsec) = explode(':', $comments_updatetime);
      
      if ($_GET[cmd]== "edit" and $_GET[comments_id] == $comments_id) {
      
      $comments_content=str_replace("<br>","\r\n",$r[comments_content]);
      echo "<tr>";
      echo "<form action='handover_index.php' method='post'>";
      echo "<td class='txt' valign='top' width='375'> <font class='rowadded'>Added</font> <font class='rowaddeddata'>$comments_date $thour:$tmin</font> - $comments_user";
      echo "<td class='rowtxt' valign='top' width='45'><div class='rowalign'><input type=submit name=submit value=Update><input type=hidden name=comments_id value=$comments_id></div></td>";
      echo "<tr></tr>";
      echo "<td class='rowtxt' valign='top' colspan='2' width='100%'><textarea name=comments_content cols='10' rows='5'>$comments_content</textarea><br><br></td>";
      echo "</form>";
      echo "</tr>";
    
      }else{ if($i%2 == 0){
 
        echo "<tr class='rowresult1'>";
        echo "<td class='txt' valign='top' width='375'> <font class='rowadded'>Added</font> <font class='rowaddeddata'>$comments_date $thour:$tmin</font> - $comments_user";
        if (!empty($comments_updateuser)) {
            echo "<font size='2' color='#000000'> / </font> <font class='rowadded'> Updated</font> <font class='rowaddeddata'>$comments_updatedate $uthour:$utmin</font> - " .$comments_updateuser. "";
        }
        echo "</td>";
        echo "<td class='rowtxt' valign='top' width='45'><div class='rowalign'><INPUT TYPE='button' name='edit' value='Edit' onClick=self.location='handover_index.php?cmd=edit&comments_id=$comments_id&search=$search'></div></td>";
        echo "</tr><tr class='rowresult1'>";
        echo "<td class='rowtxt' colspan='2' valign='top' width='100%'>$comments_content<br><br>";
        echo "</td></tr>";
        $i++;
        
        }else{
        
        echo "<tr class='rowresult2'>";
        echo "<td class='txt' valign='top' width='375'> <font class='rowadded'>Added</font> <font class='rowaddeddata'>$comments_date $thour:$tmin</font> - $comments_user";
        if (!empty($comments_updateuser)) {
            echo "<font size='2' color='#000000'> / </font> <font class='rowadded'> Updated</font> <font class='rowaddeddata'>$comments_updatedate $uthour:$utmin</font> - " .$comments_updateuser. "";
        }
        echo "</td>";
        echo "<td class='rowtxt' valign='top' width='45'><div class='rowalign'><INPUT TYPE='button' name='edit' value='Edit' onClick=self.location='handover_index.php?cmd=edit&comments_id=$comments_id&search=$search'></div></td>";
        echo "</tr><tr class='rowresult2'>";
        echo "<td class='rowtxt' colspan='2' valign='top' width='100%'>$comments_content<br><br>";
        echo "</td></tr>";
        $i++;
        }
      }
    }
echo "</table>";
echo "<br>";
}else{
// ##### Search DB ##### End <-
julzk
Forum Newbie
Posts: 23
Joined: Fri Oct 30, 2009 4:42 am

Re: PHP search multiple input field box help

Post by julzk »

WOOT!! I fixed it! now works fine! :D

I had to set a variable for textsearch so Form looks like this now:

Code: Select all

      echo "<form method='post' action='handover_index.php'>";
      echo "<input type='text' name='textsearch' style='width: 60px;' maxlength='10'> ";
      echo " <input type='text' name='search' id='datedisplay' style='width: 60px;' maxlength='10'> ";
      echo "<input type='hidden' name='cmd' value='search' />";
      echo " <input type='Submit' name='submit' value='Go!'>";
      echo "</form>";
          $textsearch=$_POST["textsearch"];
Next, the SQL query had to change slightly. Where the if statement is inside the query, I changed it from if(isset to if(!empty and it works fine! :)

Code: Select all

// ##### Search DB ##### Start ->
$str = str_replace("\n", "<BR />", $str);
if (strlen($_POST["search"]) > 1 or strlen($_GET["search"]) > 1)
{
if(isset($_POST["search"])) $search=$_POST["search"];
if(isset($_GET["search"])) $search=$_GET["search"];
  
     $query = "SELECT * FROM tbl_comments ";
     
     if(!empty($_POST["textsearch"])) {
         $query .= "WHERE comments_content LIKE '%$textsearch%'
                   ORDER BY comments_date DESC, comments_time DESC";
     } else {
         $query .= "WHERE comments_date LIKE '%$search%'
                   ORDER BY comments_date DESC, comments_time DESC";
     }
     $result = mysql_query($query);
 
echo "<table width='98%' cellspacing='0' cellpadding='3' border='0'>";
 
$i = 0;
while($r=mysql_fetch_array($result))
{   
      $comments_id=$r["comments_id"];
      $comments_date=$r["comments_date"];
      $comments_time=$r["comments_time"];
      $comments_user=$r["comments_user"];
      $comments_content=str_replace("\r\n","<br>",$r[comments_content]);
      $comments_updateuser=$r["comments_updateuser"];
      $comments_updatedate=$r["comments_updatedate"];
      $comments_updatetime=$r["comments_updatetime"];
      
      list($thour, $tmin, $tsec) = explode(':', $comments_time);
      list($uthour, $utmin, $utsec) = explode(':', $comments_updatetime);
      
      if ($_GET[cmd]== "edit" and $_GET[comments_id] == $comments_id) {
      
      $comments_content=str_replace("<br>","\r\n",$r[comments_content]);
      echo "<tr>";
      echo "<form action='handover_index.php' method='post'>";
      echo "<td class='txt' valign='top' width='375'> <font class='rowadded'>Added</font> <font class='rowaddeddata'>$comments_date $thour:$tmin</font> - $comments_user";
      echo "<td class='rowtxt' valign='top' width='45'><div class='rowalign'><input type=submit name=submit value=Update><input type=hidden name=comments_id value=$comments_id></div></td>";
      echo "<tr></tr>";
      echo "<td class='rowtxt' valign='top' colspan='2' width='100%'><textarea name=comments_content cols='10' rows='5'>$comments_content</textarea><br><br></td>";
      echo "</form>";
      echo "</tr>";
    
      }else{ if($i%2 == 0){
 
        echo "<tr class='rowresult1'>";
        echo "<td class='txt' valign='top' width='375'> <font class='rowadded'>Added</font> <font class='rowaddeddata'>$comments_date $thour:$tmin</font> - $comments_user";
        if (!empty($comments_updateuser)) {
            echo "<font size='2' color='#000000'> / </font> <font class='rowadded'> Updated</font> <font class='rowaddeddata'>$comments_updatedate $uthour:$utmin</font> - " .$comments_updateuser. "";
        }
        echo "</td>";
        echo "<td class='rowtxt' valign='top' width='45'><div class='rowalign'><INPUT TYPE='button' name='edit' value='Edit' onClick=self.location='handover_index.php?cmd=edit&comments_id=$comments_id&search=$search'></div></td>";
        echo "</tr><tr class='rowresult1'>";
        echo "<td class='rowtxt' colspan='2' valign='top' width='100%'>$comments_content<br><br>";
        echo "</td></tr>";
        $i++;
        
        }else{
        
        echo "<tr class='rowresult2'>";
        echo "<td class='txt' valign='top' width='375'> <font class='rowadded'>Added</font> <font class='rowaddeddata'>$comments_date $thour:$tmin</font> - $comments_user";
        if (!empty($comments_updateuser)) {
            echo "<font size='2' color='#000000'> / </font> <font class='rowadded'> Updated</font> <font class='rowaddeddata'>$comments_updatedate $uthour:$utmin</font> - " .$comments_updateuser. "";
        }
        echo "</td>";
        echo "<td class='rowtxt' valign='top' width='45'><div class='rowalign'><INPUT TYPE='button' name='edit' value='Edit' onClick=self.location='handover_index.php?cmd=edit&comments_id=$comments_id&search=$search'></div></td>";
        echo "</tr><tr class='rowresult2'>";
        echo "<td class='rowtxt' colspan='2' valign='top' width='100%'>$comments_content<br><br>";
        echo "</td></tr>";
        $i++;
        }
      }
    }
echo "</table>";
echo "<br>";
}else{
// ##### Search DB ##### End <-
Post Reply