I’m stuck with this bit of code. By all accounts it should work, I’ve been staring at it for 2 hours now, I have no idea what is wrong.
I have two selections with options in divs. When one is selected with the right parent id, the sub category should appear, it does not for some reason though I feel I’ve done it all right – here is my html/php for the main page:
<li>
<label for="Catid">Pick a Category:</label>
<select id="Catid">
<?php
$Products = New Products();
$Products->form_Cat_Picker();
?>
</select>
</li>
<li>
<label for="Subcatid">Sub Category:</label>
<?php
$Products = New Products();
$Products->form_Subcat_Picker();
?>
</li>
<script type="text/javascript">
$(document).ready(function() {
$('#Catid').change(function(){
var optvalue = $(this).val(),
div = $('#' + 'parentid' + optvalue);
$('div').hide();
div.show();
});
});
</script>
<li>
And here are the two functions:
function form_Cat_Picker() {
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
if (!$mysqli) {
die('There was a problem connecting to the database.');
}
$catPicker = "SELECT Catid, Catname
FROM ProductCats
ORDER BY Catid";
if ($Result = $mysqli->query($catPicker)){
if (!$Result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
echo '<option value="">'.''.'</option>';
while ($row = $Result->fetch_assoc()) {
echo '<option value="'.$row["Catid"].'">'.$row["Catname"]."</option>";
echo '</div>';
}
}
$mysqli->close();
}
function form_Subcat_Picker() {
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
if (!$mysqli) {
die('There was a problem connecting to the database.');
}
$catPicker = "SELECT Subcatid, Subcatname, Parentid
FROM ProductSubCats
ORDER BY Subcatid";
if ($Result = $mysqli->query($catPicker)){
if (!$Result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$counter = 0;
while ($row = $Result->fetch_assoc()) {
if ($counter >= 1) {
if ($last_id != $row['Parentid']){
echo '</select>';
echo '</div>';
}
}
if ($last_id != $row['Parentid']){
echo '<div id="parentid'.$row['Parentid'].'" style="display:none">';
echo '<select id="Subcatid">';
}
echo '<option value="'.$row["Subcatid"].'">'.$row["Subcatname"]."</option>";
$last_id = $row['Parentid'];
$counter++;
}
echo '</select>';
echo '</div>';
}
$mysqli->close();
}
These output the following:
<li>
<label for="Catid">Pick a Category:</label>
<select id="Catid">
<option value=""/>
<option value="1">Sample 1</option>
<option value="2">Sample 2</option>
<option value="3">Sample 3</option>
<option value="4">Sample 4</option>
<option value="5">Sample 5</option>
<option value="6">Sample 6</option>
<option value="7">Sample 7</option>
</select>
</li>
<li>
<label for="Subcatid">Sub Category:</label>
<div id="parentid1" style="display: none;">
<select id="Subcatid">
<option value="1">Sub Sample 1</option>
<option value="2">Sub Sample 2</option>
</select>
</div>
</li>
So the output looks fine. The jquery seems correct.
But when I try to run it – the div id parentid1 does not appear when Sample 1 is selected.
Am I going blind or have I done something wrong? Thanks
EDIT
even works in jfiddle:
http://jsfiddle.net/VGDwm/
And I called in the jquery library properly with:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"/>
What could be wrong? Should the jquery be after or before the divs? Or is this an issue with calling in from php?
so the line highlighted here: