Jump to content

Search returns values on page load


dr-manhattan

Recommended Posts

Hi,

 

I'm a new user, so be gentle with me!

 

OK, I've got a really simple search set up, which works fine EXCEPT that all of my results are being returned when the page loads.  How do I get this search to return values ONLY when the search button is clicked?

 

I guess the answer is fairly simple, but I just can't see it!  Any help?

 

Here's the code:

 

<?php
    require("dbinfo.php");
    $search = mysql_real_escape_string($_GET["search"]);
?>
      
<form method="get">
    <input type="text" name="search" value="<?php echo $search; ?>"> 
    <input type="submit" value="Search">
</form>

<br/>

<?php
    $result = mysql_query("SELECT DISTINCT tv_brand, remote_code FROM remotecodes WHERE LOWER (tv_brand) LIKE LOWER ('%$search%')");
    $num=mysql_numrows($result);
      
    $i=0;
        while ($i < $num) 
        {$tvbrand  = htmlspecialchars(mysql_result($result, $i, "tv_brand"));
         $remotecode  = htmlspecialchars(mysql_result($result, $i, "remote_code"));
?>
    
<p>
    <?php echo $tvbrand; ?>: 
    <?php echo $remotecode; ?>
</p>
   
<?php
    $i++;
        }
    mysql_close();
?>
       
<p>
    <strong>
        <?php echo $num; ?> code(s) found
    </strong>
</p>

Link to comment
https://forums.phpfreaks.com/topic/188302-search-returns-values-on-page-load/
Share on other sites

Add a hiden input to your form, name it something like "act" or "do", set it's value to something like "search", then make a switch statement using the value of do:

 

psuedo-code:

<?php
$do = $_GET['do'];

switch($do){
  case "search":
    // Do search stuff
    break;
  default:
    // Print the form
   break;
  }
?>

 

Of course, you'll want to also include the rest of your code, this was just a simple example, I don't want to give you the answer, you'll have to figure out a bit on your own ;)

Give name to submit button and write your search code in isset of submit button.

<form method="get">
    <input type="text" name="search" value="<?php echo $search; ?>"> 
    <input type="submit" value="Search" name="btnSubmit">
</form>

 

<?php
if (isset($_POST['btnSubmit']))
{
//you search code
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.