I am using jQuery to load the search results from a php class when the user enters a search term. The problem is that jQuery runs and executes the functions as required the first time round on the main page, but when the user enters a search keyword and the results are loaded from the class file on the same page via ajax, jQuery does not execute the function required.
What I want to do is, when the search results are loaded on the main page, it should only show the Quote and the author, not the arabic and reference. When the user clicks on the Quote, it should act like an accordian, toggling the div container of arabic and reference.
However, while jQuery works great loading the results from the class file, it doesn't execute to hide the container of arabic and reference when it's loads the search results nor does it run the click event when the user clicks on the Quote to show the container, as firebug shows.
index.php:
<script type="text/javascript">
$(function() {
$(".bsearch")
.keydown(function() {
//create post data
var postData = {
"search" : $(this).val()
};
//make the call
$.ajax({
type: "POST",
url: "quotes_in.php",
data: postData, //send it along with your call
success: function(response){
$("#left").html(response);
}
});
}),
$("div#smore").hide();
//toggle the componenet with class msg_body
$("p#s_quotes").click(function()
{
$(this).next("div#smore").slideToggle(200);
}),
});
</script>
<!-- Begin Left Column -->
<div id="left">
</div>
<!-- End Left Column -->
Quotes_in.php:
include_once "inc/class.quotes.inc.php";
$quotes = new Quotes($db);
if (isset($_POST['search']))
$quotes->searchQuotes();
The function in the class file which 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>";
}