I am trying to develop a seach page to search the customer files that in the database. I want to be able to search and the result display on the same page and i can search for another customer without going to another page.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">

$(function() {

    $(".search_button").click(function() {
        // getting the value that user typed
        var searchString    = $("#search_box").val();
        // forming the queryString
        var data            = 'search='+ searchString;

        // if searchString is not empty
        if(searchString) {
            // ajax call
            $.ajax({
                type: "POST",
                url: "getdev.php",
                data: data,
                beforeSend: function(html) { // this happens before actual call
                    $("#results").html('');
                    $("#searchresults").show();
                    $(".word").html(searchString);
               },
               success: function(html){ // this happens after we get results
                    $("#results").show();
                    $("#results").append(html);
              }
            });   
        }
        return false;
    });
});
</script>


</head>
<body>
<?php include('header.html.php'); ?>
<div id="content">
    <div id="columnC">
        <h2>Search Customers</h2>

<form method="post" action="getdev.php">
    <input type="text" name="search" id="search_box" class='search_box'/>
    <input type="submit" value="Search" class="search_button" /><br />
</form>


      <div id="searchresults">Search results :</div>

        </div>



</div>

so this is php file 
<?php
//if we got something through $_POST
if (isset($_POST['search'])) {
    // here you would normally include some database connection
    include('connect.php');
   // $db = new db();
    // never trust what user wrote! We must ALWAYS sanitize user input
    $word = mysql_real_escape_string($_POST['search']);
    $word = htmlentities($word);
    // build your search query to the database
    $q = "SELECT custid, cdate, custName, address1, address2, phone, cellphone, email, equipment, manufacturer, model, serial, imei, notes, priority FROM customer WHERE custid='$word' OR custName like '$word' ORDER BY equipment ASC ";      
$result = @mysqli_query ($dbcon, $q); // Run the query.
if ($result) { // If it ran, display the records.
// Table header.
echo '<table id="result"> <tr>
<th><b>Edit</b></th>
<th><b>Delete</b></th>
<th><b>ID</b></th>
<th><b>Date</b></th>
<th><b>Name</b></th>
<th><b>Address</b></th>
<th><b>Address </b></th>
<th><b>Phone </b></th>
<th><b>Cellphone </b></th>
<th><b>Email</b></th>
<th><b>Equipment</b></th>
<th><b>Manufacturer</b></th>
<th><b>Modell</b></th>
<th><b>Serial</b></th>
<th><b>IMEI</b></th>
<th><b>Notes</b></th>
<th><b>Priority</b></th>
</tr>';

    } else {
        echo '<li>No results found</li>';
    }
}
?>

I am not getting any result on the page at all. Any help or link to an example would be lovely

Hi Ryan,

  • echo the query and check if the final query is correct - you can also try to run the query in phpmyadmin and see if it returns any results.
  • if you don't have access to phpmyadmin, try removing the suppress error operator '@' when you run the query.
  • call mysqli_error() after you run the query, this will give you more details if something went wrong
  • depending on your server settings, you might need to turn on error reporting

    error_reporting(E_ALL); // report all errors
    ini_set('display_errors', 1); // send to output

  • your query is looking for an exact match, use wildcard to search parts of the string: LIKE '%$word%'

Hi Ryan,
In your getdev.php file, I don't see where is the $result to be displayed. In your code, you are only displaying the header

Thank guys for your response, i got it to work you assistance was really appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.