I have a jquery code below which is displayed in the editsessionadmin.php page. Now what is suppose to happen is that when this page is opened, in will perform the jquery/ajax function so that it navigates to the module.php page in order to be able to select a list of modules from a query, display each module as an option and display those options in the module drop down menu in the editsessionadmin.php page.
The problem I have is that it is not doing this. The jquery/ajax is not doing anything as that the Module drop down menu in the editsessionadmin.php script does not contain any options except for the Please Select option.
Does anybody know how to fix the jquery/ajax code in order to perform what I want it to perform?
Below is the editsessionadmin.php code:
<script type="text/javascript">
$(document).ready( function(){
function getModules() {
jQuery.ajax({
type: "post",
url: "module.php",
success: function(response){
jQuery('#modulesDrop').append(response);
}
});
});
}
</script>
....
<?php
$moduleHTML = "";
$moduleHTML .= '<select name="modules" id="modulesDrop">'.PHP_EOL;
$moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$moduleHTML .= '</select>';
?>
module.php page:
`
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
$moduleactive = 1;
$sql = "SELECT ModuleId, ModuleNo, ModuleName FROM Module WHERE ModuleActive = ? ORDER BY ModuleNo";
$sqlstmt=$mysqli->prepare($sql);
$sqlstmt->bind_param("i", $moduleactive);
$sqlstmt->execute();
$sqlstmt->bind_result($dbModuleId,$dbModuleNo,$dbModuleName);
$moduleHTML = "";
while($sqlstmt->fetch()) {
$moduleHTML .= sprintf('<option value="%1$s_%2$s_%3$s">%1$s - %2$s</option>'.PHP_EOL, $dbModuleNo, $dbModuleName, $dbModuleId);
}
echo $moduleHTML;
$sqlstmt->execute();
?>`
Your code looks like it is creating a function on document ready, but never calling it. Also it looks like you have your brackets a little out of order. This becomes a little more obvious by indenting the code.
What you could do is delete the line
function getModules() {and the spare}at the end, and then it should work or at least give you another GET in FireBug.