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
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 ;)

Link to comment
Share on other sites

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
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.