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
- search.html and
- 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><html>
<body>
<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/>
</form>
</body>
</html>
2. search-ajax.html
<html><head>
<script>
function doSearch(str)
{
if (str.length == 0)
{
document.getElementById("txtSearchResult").innerHTML = "";
return;
}
else
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("txtSearchResult").innerHTML = xmlhttp.responseText; }
}
xmlhttp.open("GET", "search.php?txtName=" + str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<center>
<form>
Search Name: <input type="text" onkeyup="doSearch(this.value)">
</form>
<p>
<span id="txtSearchResult"></span>
</p>
</center>
</body>
</html>
3. search.php
<?phprequire "conn.php";
try
{
$name="%".$_GET["txtName"]."%";
$stmt = $conn->prepare("SELECT id, name, email, phone FROM tbl where name LIKE ?");
$stmt->execute(array($name));
$result = $stmt->fetchAll();
if( ! $result)
{
print('No Records Found');
}
else
{
/*
$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>";
$search_str=$row['id'];
echo "<a href='edit.php?id=$search_str'>Edit</a>";
echo "</td>";
echo "<td>";
$search_str=$row['id'];
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>";
?>
This example is divided in multiple part.
Part-1: Configure and Setup the MySQL database connection using PDO.
Part-2: Create a MySQL Database and a table using PDO.
Part-3: Insert record using prepared statement in PDO.
Part-4: Display Records using prepared statement.
Part-5: Search records using AJAX and PDO in PHP.
Part-6: Update Record using prepared statement in PDO.
Part-7: Delete Record using prepared statement in PDO.
Subscribe to:
Post Comments (Atom)
Subjects
- WordPress
- Mobile Computing-4649303 Practical Solution
- Android Programming New Syllabus Theory
- PHP LAMP Question Bank
- PHP LAMP Theory
- Step by Step Android Example
- Android Practical
- Android Theory
- Android Question Bank
- Networking FON Practical
- Networking FON Theory
- OS Practical
- OS Theory
- HTML
- JavaScript
- J2EE WTAD Theory
- J2EE WTAD Question Bank
- J2EE WTAD Quick Guide
- J2EE WTAD GTU Papers
- J2EE WTAD Practical
- Python
- JAVA Theory
- JAVA Practical
- MIS
Categories
- Android (55)
- c (11)
- Configure Tomcat7 (2)
- CSS (3)
- Decryption (16)
- Difference (1)
- Encryption (16)
- Error Detection and Correction Techniques (3)
- FON (27)
- Framing Technic (2)
- install Tomcat (2)
- J2EE (29)
- JAVA (13)
- JavaScript (19)
- linux (8)
- OS (17)
- PHP (11)
- Protocol (3)
- SERVER SOCKET PROGRAMING (7)
- Servlet (13)
- shell script (33)
- unix (22)
- WTAD (34)
Blog Archive
-
▼
2015
(13)
-
▼
July
(8)
- PHP-Delete Record using prepared statement in PDO
- PHP - Update Record using prepared statement in PDO
- PHP - Search records using AJAX and PDO
- Part-4: Display Records using prepared statement
- PHP insert update, delete using PDO part-3
- PHP insert update, delete using PDO part-2
- PHP insert update, delete using PDO part-1
- PHP PDO : insert, update delete example for beginners
-
▼
July
(8)
Total Pageviews
© BipinRupadiya.com. Powered by Blogger.
5 comments:
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
http://pakaribazar.com/webpatna.php
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
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
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
Post a Comment