I am very new to programming with jQuery. I've spent quite a while trying to move forward with this, and I've managed to get some of it done. But I've really hit a wall and I can't seem to find help from anywhere/anyone.
Scenario:
I am using a select box to store different music genres, which I have retrieved via PHP/MySQL.
<?php include 'connectingDB.php'; $catSQL = "SELECT * FROM category ORDER BY catDesc"; $queryresult = mysql_query($catSQL) or die (mysql_error()); echo "<select id= \"catID\">"; while ($row = mysql_fetch_assoc($queryresult)) { $catID = $row['catID']; $catDesc = $row['catDesc']; echo "<option value = \"$catID\">$catDesc</option>\n"; } echo "</select>"; mysql_free_result($queryresult); mysql_close($conn); ?>
When I click on a genre, I want all of the related CDs and CD information to be retrieved in JSON format and dynamically displayed in a table using AJAX (below the select box on that same page)
<?php header('Content-type: application/json'); include 'connectingDB.php'; $category = $_REQUEST['catname']; $sql = "SELECT `CDID`, `CDTitle`, `CDYear`, `pubID`, `CDPrice` FROM `tiptop_cd` INNER JOIN tiptop_category ON tiptop_cd.catID=tiptop_category.catID WHERE catDesc = '{$category}'"; $result = mysqli_query($con,$sql); $row = mysqli_fetch_array($result); while($row = mysqli_fetch_array($result)){ $returned[] = $row; } echo json_encode($returned);
?>
All of the above code works on its own. But I'm looking to connect it all together. I think it needs to be via an
onchange
event in jQuery?I've got an
alert
to pop up after clicking a category, but that's as far as I can get..$(document).ready(function(){ $("#catID").change(function(){ alert("The text has been changed."); }); });
Does it need to be in a foreach
loop? Or a foreach
within a foreach
?
To summarize, I'm trying to understand how to: display the cds and cd information that are related to the specific category that is currently selected, in a dynamic table with ajax
Any help is massively appreciated.