postheadericon PHP - Search records using AJAX and PDO

Part-5: Search records using AJAX and PDO in PHP

In part five, we do the search operation on our MySQL table. We use prepared statement to execute our query using PHP-PDO object. In SQL query we use like operator so it match record like the input we get from search.html file. To do the search object we use the AJAX technology to pass the request to search.php file. The file search.php is exactly similar to display.php file, just a where condition is added to do the search operation.  If we do the smart work then one file display.php or search.php is enough to do both operation just an if condition is required that check the request parameter whether search criteria is provided or not? To keep it simple I have use to separate file that would be helpful to beginners.

In search operation we use two file

  1. search.html and 
  2. search.php

for search.html, I am writing two code. One that does not use AJAX to make the simple search and Second then utilize the power of AJAX in search operation.

1. search.html 

<!DOCTYPE html>
<form method="GET" action="search.php">
Name  :<input type="text" name="txtName" required><br/>
<input type="submit" value="Search">
<input type="reset" value="Clear"><br/>

2. search-ajax.html

function doSearch(str)
     if (str.length == 0)
          document.getElementById("txtSearchResult").innerHTML = "";      
          var xmlhttp = new XMLHttpRequest();      
          xmlhttp.onreadystatechange = function()
                  if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                         document.getElementById("txtSearchResult").innerHTML = xmlhttp.responseText;                      }      
       }    "GET", "search.php?txtName=" + str, true);      
          Search Name: <input type="text" onkeyup="doSearch(this.value)">
<span id="txtSearchResult"></span>

3. search.php

require "conn.php";
$stmt = $conn->prepare("SELECT id, name, email, phone FROM tbl where name LIKE ?");


$result = $stmt->fetchAll();

if( ! $result)
print('No Records Found');
$cc=$stmt->columnCount(); // count total number of columns
$rc=$stmt->rowCount();  // count total number of rows
echo "row count = $rc";
echo "Column count = $cc";
echo "<table border='1' align='center' cellpadding='5px' cellspacing='2px'>";
//while($row = $stmt->fetch())
echo "<tr><th>Name</th><th>Email</th><th>Phone</th><th colspan='2'></th></tr>";
foreach($result as $row)
echo "<tr>";

echo "<td>";
echo $row['name'];
echo "</td>";
echo "<td>";
echo $row['email'];
echo "</td>";
echo "<td>";
echo $row['phone'];
echo "</td>";

echo "<td>";
echo "<a href='edit.php?id=$search_str'>Edit</a>";
echo "</td>";

echo "<td>";
echo "<a href='delete.php?id=$search_str'>Delete</a>";
echo "</td>";
echo "</tr>";

echo "</table>";
catch(PDOException $e)
echo "Error: " . $e->getMessage();
$conn = null;
echo "</table>";


Unknown said...

I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
iOS Training in Chennai
Android Training in Chennai
php Training in Chennai

Priya Kannan said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
PHP Training in Chennai

Website Development Service said...

Madhu Bala said...

I would like to say that this blog really convinced me to update my knowledge about the technology you talk about. Thanks, very good post
php course
php courses in chennai

Anjali Siva said...

Outstanding blog with lots of information. Keep posting more like this.
ccna course in Chennai
ccna institute in Chennai
ccna Training center in Chennai
gst classes in Chennai
ReactJS Training in Chennai
Salesforce Training in Chennai
ccna course in Tambaram
ccna course in Velachery
CCNA course in Vadapalani

chandhran said...

This was helpful to me thanks for shring this blog...kindly continue this blog..
Aviation Academy in Chennai
Air Hostess Training in Bangalore
air hostess course in mumbai
Aviation Academy in Chennai
Air hostess training in Bangalore
Aviation Academy in Chennai
air hostess training in chennai
Aviation Academy in Chennai
Aviation Academy in Chennai
Aviation courses in Bangalore

priya said...

best php training in chennai
best php developer institution chennai
best php training with placement in chennai
best php training center in chennai
best php course in chennai

priya said...

I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective.
best appium training in chennai
appium online training
appium training centres in chennai
appium training institutes in chennai
appium training institutes for appuim in chennai
appium training content

madhukits said...

SAP BW on Hana training
sap sd training
osb training
oracle scm training
abinitio training
spark training

Boomerang Airplane said...

Incredible post I should say and a debt of gratitude is in order for the data. Schooling is certainly a tacky subject. Be that as it may, is still among the main subjects within recent memory. I appreciate your post and anticipate more. You have made some valid statements there. I looked on the web to study the issue and discovered a great many people will oblige your perspectives on this site...
science behind paper airplanes | how to make a paper airplane that flies far and straight step by step | classic dart paper airplane | zazoom internet | windfin | how to make the coolest paper airplane | how to make a good paper airplane that flies far | best paper model | the best glider paper airplane

Total Pageviews

© Powered by Blogger.