I use jQuery to load search results from a php class when a user types in a search query. The problem is that jQuery starts and executes functions as needed for the first time on the main page, but when a user enters a search keyword and the results are loaded from a class file on the same page through ajax, jQuery does not execute the required function.
What I want to do, when the search results are loaded on the main page, it should show only the quote and the author, and not the Arabic and link. When the user clicks on Quote, he should act as an accordion, switching the Arabic div container and link.
However, although jQuery works fine with loading results from a class file, it does not execute to hide the Arabic language container and the link when it loads the search results, and does not fire a click event when the user clicks on the link to show the container, as firebug shows.
index.php:
<script type="text/javascript">
$(function() {
$(".bsearch")
.keydown(function() {
var postData = {
"search" : $(this).val()
};
$.ajax({
type: "POST",
url: "quotes_in.php",
data: postData,
success: function(response){
$("#left").html(response);
}
});
}),
$("div#smore").hide();
$("p#s_quotes").click(function()
{
$(this).next("div#smore").slideToggle(200);
}),
});
</script>
<div id="left">
</div>
Quotes_in.php:
include_once "inc/class.quotes.inc.php";
$quotes = new Quotes($db);
if (isset($_POST['search']))
$quotes->searchQuotes();
A function in the class file that formats the search:
---SQL QUERY for SEARCH---
public function formatSearch($row, $search)
{
return "<div class=\"swrap\">"
. "<p id=\"s_quotes\"> " . $cQuote . " </p>"
. "<div id=\"smore\"><p id=\"s_arabic\">" . $this->h($row['cArabic']) . "</p>"
. "<p id=\"s_reference\"><span class=\"source\">Source:</span> " . $this->h($row['vReference']) . "</p></div>"
. "<p id=\"s_author\"><b>-</b> " . $this->h($row['vAuthor']) . "</p></div>";
}
input source
share