dr-manhattan Posted January 13, 2010 Share Posted January 13, 2010 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 More sharing options...
HaLo2FrEeEk Posted January 13, 2010 Share Posted January 13, 2010 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 https://forums.phpfreaks.com/topic/188302-search-returns-values-on-page-load/#findComment-994075 Share on other sites More sharing options...
PravinS Posted January 13, 2010 Share Posted January 13, 2010 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 https://forums.phpfreaks.com/topic/188302-search-returns-values-on-page-load/#findComment-994077 Share on other sites More sharing options...
HaLo2FrEeEk Posted January 13, 2010 Share Posted January 13, 2010 Switch statements are probably a lot cleaner than using an if...then statement because you can add to them much easier, but whatever, both will work fairly similarly. Link to comment https://forums.phpfreaks.com/topic/188302-search-returns-values-on-page-load/#findComment-994078 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.