search form

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

I think your looking for something like this.

[php]

Search Contacts

Search Contacts Details

You may search either by first or last name

[/php]

hello

thanks but more than HTML form code i need the ajax script or post script. my HTML code is fine

To do this you’re going to need 3 scripts. One for display (IE: the form in HTML) one for fetching the info from the database (php) and one to read that file (javascript/jquery/etc.)

I have included a brief example below of how things flow.

The php file just needs to talk to the database and echo out the result;
[php]// ajax.php

$searchfor = $_GET[‘d’];
$query = “SELECT something FROM database WHERE name = ?”
if($stmt = $sql->prepare($query)) {
$stmt->bind_param(‘s’, $searchfor);

continue code here…

}

echo $data;
[/php]

The HTML file has a div where we will place the result;

<div id="showme"></div>

The javascript file will pass the variable using ‘get’ to the php script, copy the echo’d data and place it in the div;

$(document).ready(function() { $.get('ajax.php?d=smith', function(data) { $('#showme').html(data); });

Hope that helps,
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service