Jump to content

search form


sam20e

Recommended Posts

hi  im new to these stuff and appreciate any help.  

 

im creating a simple form with 1 textbox and a search button. when the button is pressed a select query should run and check the mysql database and list that result on that same page.  For example, lets say there is a textbox named Lastname, then the search button should search mysql and list down the last name matching that name.  

 

Form code : 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     <title>Search</title> </head> <body>     <form action="post.php" method="GET">         <input type="text" name="query" />         <input type="submit" value="Search" />     </form> </body> </html>
 

 

Any help? I need to show the result right bellow the button, and if no match found it should display an error : no match found 

 

thanks

Link to comment
Share on other sites

what code have you tried? we can really only help you with your code after you have actually made an attempt.

 

php was originally created to do exactly what you are trying, process form data. there are probably two million examples posted on the web and a few 10's of thousands of books published on this subject for you to find and read to learn the basics.

 

for the php mysql database library functions, you should be using prepared queries with either the PDO (preferred) or mysqli_ functions, because the msyql_ functions are obsolete. 

Link to comment
Share on other sites

Hi

 

Thank you for the reply. This is what I have so far :

HTML
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Search</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
    <form action="post.php" method="GET">
        <input type="text" name="text" />
        <input type="submit" value="Search" />
    </form>
</body>
</html>
PHP
<?php
mysql_connect("localhost", "db", "pass") or die("Connection Failed");
mysql_select_db("db")or die("Connection Failed");
$text= $_POST['text'];
$query = "select * from table where text= '$text'";
$result = mysql_query($query);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "Name: ".$line['name'];
echo " Age: ".$line['age'];
echo "<br>\n";
}
?>

It works great, but this is displaying result in a new window, i want the result to be shown bellow my button and also need to show no records found if the query result is null/na

Edited by sam20e
Link to comment
Share on other sites

 

 

It works great, but this is displaying result in a new window, i want the result to be shown bellow my button and also need to show no records found if the query result is null/na

 

Then you need to look into JavaScript and AJAX.

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.