• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

Search function that create s pop-up if value is not entered.

I have a search function that is will not execute if a zip code is not entered.   Therefore I need to have a pop-up probably javascript that tells the user that they must input a zip code to search by.  It should be a very simple piece of code however I have been unsuccessful those far.  Here is the code that I think is relevant.

 
         //Add zip code search
        if(!empty($_REQUEST['zip'])) {
            $search[] = 'ZIP = '.$_REQUEST['zip'];
         //Add Price From
         if(!empty($_REQUEST['pfrom']) && ($_REQUEST['pfrom'] != '-1'))
             $search[] = 'price >= '.$_REQUEST['pfrom'];
         //Add Max Price
         if(!empty($_REQUEST['pto']) && ($_REQUEST['pto'] != '-1'))
               $search[] = 'price <= '.$_REQUEST['pto'];          
         //Add Property ID
           if(!empty($_REQUEST['id']))
               $search[] = 'id = '.$_REQUEST['id'];              
          //Add Property Type
           if(!empty($_REQUEST['cid']))
               $search[] = 'cid = '.$_REQUEST['cid'];
          //Add Min Bed
           if(!empty($_REQUEST['bed']))
               $search[] = 'bed >= "'.$_REQUEST['bed'].'"';
          //Add Min Bath
           if(!empty($_REQUEST['bath']))
                $search[] = 'bath >= "'.$_REQUEST['bath'].'"';        

         //implode to search string on ' and ';
          $searchStr = @implode(' and ',$search);
          $str = "";
          $searchStr .= $str;
          $sql = 'select * FROM rentals WHERE (expires > NOW()) and active = "Yes" and ';
          $sql .= $searchStr;
   
      
                //Add column sorting
            if($_REQUEST['sort'] != '')
                  $sort = ' order by price asc ';
            else
                  $sort = $_REQUEST['sort'];
            
            ### DEBUG
            if($debugP) echo 'Advanced Search Sql<hr>'.$sql;
            
            $error['Results'] = 'No results found, please search again';
}      
      ### Finished Building search sql and execting #####
      $sql .= $sort . $limit;
                           
      //Perform search
      $searchResults = $mysql->exSql($sql);
      print $sql;

dont know if I need to include an else statement  in that first search parameter for zipcode.  I would assume i need to change !empty to isset but obviously need your help.  thanks
0
mnoel76
Asked:
mnoel76
  • 6
  • 6
1 Solution
 
BogoJokerCommented:
Hi mnoel76,

1) You can redirect them back to the previous page if zip is empty
2) javascript you can validate it when the submit the form, but I would need to see your form to help you out a little better.
3) you should also validate it  in php, here is an example

if (preg_match('/^[\d]{5}$/', $search[0] == 0)
{
  // Bad Zip code, it failed to be exactly 5 numbers
  header("Location: index.php"); // <-- change the index.php to whatever page you want.
}


Joe P
0
 
mnoel76Author Commented:
I guess I should also include...when I click the sublit button with no value in the zipcode, it will execute a script that I have to populate my categories drop down box.  As well as return results as if it found a property.   I guess i could add a die statement in the if  statement to prevent if from execute that sql statement.
0
 
BogoJokerCommented:
Javascript validation would be very similiar, here is an example:
<html>
<head>
<script type="text/javascript">
function validate()
{
  var regex = /^[0-9]{5}$/;
  if (regex.test(document.getElementById("zip").value))
    return true;
  alert("Zip Code must be just 5 numbers!");
  return false;
}
</script>
</head>

<body>
<form>
Enter ZipCode: <input type="text" id="zip" size=5>
<input type="submit" value="submit" onClick="return validate();
>
</form>
</body>
</html>

Really quick and dirty validation.  Regex /^[0-9]{5}$/ Means the zipcode must be 5 numbers and absoluteely nothing else.  Same as \d (stands for digit).  But as always, people may have turned off javascript so you should validate it with php, like I showed you above.
Joe P
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mnoel76Author Commented:
here is my form.   I will try what you suggested right now and see if it works.

#Fetch Property Types From Database
      $sql = 'select * from categories order by title asc';
      $ptResults = $mysql->exSql($sql);
      
      #Build Select Options for Queries above
      
      while($pt = mysql_fetch_assoc($ptResults))
            $ptOptions .= '<option value="'.$pt['id'].'">'.$pt['title'].'</option>';
            
?>
<link href="mystyle.css" rel="stylesheet" type="text/css">
<form action="sm_search.php" name="small_searchS" method="post">
 <table width="100%" border="0" cellspacing="0" cellpadding="1">
           <tr valign="middle">
           <td class="searchBoxText" align="left">&nbsp;&nbsp;Zip Code:</td>
            <td><input name="zip" type="text" id="zip" size="6">
              </td>
          </tr>
           
       
           <td class="searchBoxText" align="left">&nbsp;&nbsp;Min Price:&nbsp;</td>
               <td><select name="pfrom" id="pfrom">
                <option value="-1"><? echo Money("0"); ?></option>
                <option value="25000"><? echo Money("25000"); ?></option>
                <option value="50000"><? echo Money("50000"); ?></option>
                <option value="75000"><? echo Money("75000"); ?></option>
                <option value="100000"><? echo Money("100000"); ?></option>
                <option value="125000"><? echo Money("125000"); ?></option>
                <option value="150000"><? echo Money("150000"); ?></option>
                <option value="175000"><? echo Money("175000"); ?></option>
                <option value="200000"><? echo Money("200000"); ?></option>
                <option value="250000"><? echo Money("250000"); ?></option>
                <option value="300000"><? echo Money("300000"); ?></option>
                <option value="350000"><? echo Money("350000"); ?></option>
                <option value="400000"><? echo Money("400000"); ?></option>
                <option value="450000"><? echo Money("450000"); ?></option>
                <option value="500000"><? echo Money("500000"); ?></option>
                <option value="600000"><? echo Money("600000"); ?></option>
                <option value="700000"><? echo Money("700000"); ?></option>
                <option value="800000"><? echo Money("800000"); ?></option>
                <option value="900000"><? echo Money("900000"); ?></option>
                <option value="1000000"><? echo Money("1000000"); ?></option>
                <option value="2000000"><? echo Money("2000000"); ?></option>
                <option value="3000000"><? echo Money("3000000"); ?></option>
              </select></td>
          </tr>
          <tr>
           <td class="searchBoxText" align="left">&nbsp;&nbsp;Max Price:&nbsp;</td>
                <td><select name="pto" id="pto">
                        <option value="-1">All</option>
                <option value="25000"><? echo Money("25000"); ?></option>
                <option value="50000"><? echo Money("50000"); ?></option>
                <option value="75000"><? echo Money("75000"); ?></option>
                <option value="100000"><? echo Money("100000"); ?></option>
                <option value="125000"><? echo Money("125000"); ?></option>
                <option value="150000"><? echo Money("150000"); ?></option>
                <option value="175000"><? echo Money("175000"); ?></option>
                <option value="200000"><? echo Money("200000"); ?></option>
                <option value="250000"><? echo Money("250000"); ?></option>
                <option value="300000"><? echo Money("300000"); ?></option>
                <option value="350000"><? echo Money("350000"); ?></option>
                <option value="400000"><? echo Money("400000"); ?></option>
                <option value="450000"><? echo Money("450000"); ?></option>
                <option value="500000"><? echo Money("500000"); ?></option>
                <option value="600000"><? echo Money("600000"); ?></option>
                <option value="700000"><? echo Money("700000"); ?></option>
                <option value="800000"><? echo Money("800000"); ?></option>
                <option value="900000"><? echo Money("900000"); ?></option>
                <option value="1000000"><? echo Money("1000000"); ?></option>
                <option value="2000000"><? echo Money("2000000"); ?></option>
                <option value="3000000"><? echo Money("3000000"); ?></option>
                <option value="5000000"><? echo Money("5000000"); ?></option>
                <option value="10000000"><? echo "Unlimited"; ?></option>
              </select></td>
          </tr>  <tr>
              <td class="searchBoxText" align="left">&nbsp;&nbsp;Bed(s)</td>
              <td><select name="bed" id="bed">
                <option value="0" selected>Any</option>
                <option value="1">1+</option>
                <option value="2">2+</option>
                <option value="3">3+</option>
                <option value="4">4+</option>
                <option value="5">5+</option>
                <option value="6">6+</option>
              </select></td>
          </tr>
          <tr>            
              <td class="searchBoxText" align="left">&nbsp;&nbsp;Baths(s)</td>
                <td><select name="bath" id="bath">
                <option value="0" selected>Any</option>
                <option value="1">1+</option>
                <option value="2">2+</option>
                <option value="3">3+</option>
                <option value="4">4+</option>
                <option value="5">5+</option>
                <option value="6">6+</option>
              </select></td>
          </tr>
         <tr>
               <td colspan="1" align="left" nowrap>
            <input type="submit" style="cursor: pointer;" name="small_search" value="For Sale"></td>
                  
            <td colspan="1" align="left" nowrap>
              <input type="submit"  style="cursor: pointer;"  name="small_search" value="Rentals"></td>
          </tr>
                     <td colspan="2" align="center" nowrap><a href="index.php?go=search_properties" class="sideLink">Advanced Search</a> &nbsp;
    </td>
  </table>
</form>
0
 
mnoel76Author Commented:
bogojoker...question for you.  Where would I put the validate function.  I the php page creates the search query??
0
 
RoonaanCommented:
You can put the <script></script> just above your <form> or inside your <head></head> section.

-r-
0
 
BogoJokerCommented:
Although I agree with -r- 100%, that the <script> be above the <form> or inside the <head> I recently tested out defining javascript anywhere in the code and the page still worked.  I have always seen that you should define all functions in the <head> so that they will be available if ever used in the body.  I believe that is the best/correct practice, but I think browsers now are more capabale and the javascript can be placed anywhere.  But obviously I endorse the same idea that -r- said, I would be an extremest and say put it in the head.  
(I was  refering to only Firefox and IE.  It is possible some browsers require what Roonaan said, so to be safe just do it that way.)
0
 
BogoJokerCommented:
Ohh, the javascript should go in the page that has the <form>.  You can write it and echo/print it in php, but you can just as easily stop php and write the <script> right in html code.
0
 
mnoel76Author Commented:
bogo

thanks for the responses .  I will test them tomorrow...and let you know the result.   Hope you are enjoying the weekend.

peace
mnoel76
0
 
BogoJokerCommented:
Sure am, I leave for Easter brunch in a few hours =)
Keep well.
0
 
mnoel76Author Commented:
Bogojoker

thanks for the help!! its working now!

thanks again,

mnoel76
0
 
BogoJokerCommented:
Great! =)
0
 
mnoel76Author Commented:
guess I forgot to accept your answer!! thanks again!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now