This works but only 80+ % of the time and I'm trying to perfect it.
I am displaying a mysql result set in a loop on the page like this:
<span class="titles">
<?php echo htmlspecialchars($row_rsTitles['title_'. $i . ''],ENT_QUOTES,'utf-8');$i++;?>
</span>
Gone with the Wind A Night to Remember Last of the Mohicans
When I click on a title, this
$(document).ready(function() {
$(".titles").click(function() {
$.ajax({
type: 'POST',
url: 'AjaxFilms.php',
data: {title: $(this).text()}
});
});
sends the title to the recordset page, which sets a session var,
if(isset($_POST['title'])) { $_SESSION['theTitle'] = $_POST['title'];}
then the recordset below uses $_SESSION['theTitle'] to select the data.
$stmtStudTitles = $con->prepare("SELECT DISTINCT(FilmTitle) FROM FilmProg WHERE title=:id ORDER BY FilmTitle ASC");
$stmtStudTitles->bindParam(':id', $_SESSION['theTitle'], PDO::PARAM_STR);
try {$stmtStudTitles->execute();} catch(PDOException $e){echo errorHandle($e);}
$numRowsstmtStudTitles = $stmtStudTitles ->rowCount();
It then uses jquery to load() the div 'Started',
$("#Started").load('https://www.example.com/AjaxFilms.php');
with the php output from the query:
<u>'.htmlspecialchars($row_rsStudTitles['FilmTitle'],ENT_QUOTES,'utf-8').'</u>
When I click on a different title, it loads different data.
This works pretty well, 80%, but not always. Those failures are simply that the data appearing in 'Started' doesn't change from the previous click, until I click a second time, and I'm trying to understand why not.
I've checked the SESSION[theTitle] and I know it is updating correctly on each click; I've tried using .on('click', function(), and I tried using GET instead of POST, but they don't really improve the situation. I tried using replaceWith(), (that got a foggy response)
I've wondered if the recordset is simply activating with the previous data before the session[theTitle] can change but I don't know much about the timing of all that stuff at all.
Can someone enlighten me as to why this could be happening and some ideas how to make this work first click, every click?
Thanks.