Web Hosting Talk







View Full Version : Working with Forms..


rocker2
05-19-2004, 07:42 AM
Hi there,

I am working on a site, where I have a search function. I am using a open source search script, phpdig for that. I wanted to know, how can set a default search words using the submit button.I mean, there on the site I have two buttons, one is for normal search when ever some on enter some text in the text box above and clicks on search it will fetch the results.And for the other button, I would like set some default words for search so that when ever some one clicks on
that.automatically the results will be fetched.

Any ideas, thanx in advance.

Regards.

Charlottezweb
05-19-2004, 10:46 AM
I'm not understanding why you would want to do that? Instead of using a form for that, why not just use a link?

-Jason

Powi
05-19-2004, 04:52 PM
<FORM METHOD="POST" ACTION="search.php">
<INPUT TYPE="HIDDEN" NAME="cat" VALUE="DEFAULT SEARCH">
<INPUT TYPE="SUBMIT" value="Search">
</FORM>

In this way you'll always send the same search (your default word).

For the other button you can leave the form you already have.

pyoor
05-24-2004, 08:23 PM
Powi's code will create the form for them to enter search words. I recommend you dont put value="bleh" and just leave that off entirely so its a blank textbox.

On the search.php (where you will be routed to once they click the Search button) you need something like the following... (I'm assuming you are using PHP and MySQL... if you use another database lemme know or just read up at www.php.net)

<?php
if (isset($_POST['cat']))
{
$searchForThis = $_POST['cat'];
$db="databasename";
$link = mysql_connect('host', 'user', 'password');
if (!$link)
die('Could not connect: ' . mysql_error());
mysql_select_db($db , $link) or die("Couldn't open $db:
".mysql_error());

$result = mysql_query("SELECT * FROM Table WHERE Field LIKE '%$searchForThis%'");
while ($row = mysql_fetch_array($result))
{
echo $row['FieldData'];
}
mysql_free_result($result);
mysql_close($link);
}
else
{
/*same as above but in the SELECT query you can put whatever you want the default query to be... alternatively you can get rid of the if (isset) syntax above and if they don't provide any search criteria it will return all records since all of them will be LIKE an empty string. */
}
?>