I have php/mysqli and jquery code below where it displays a course drop down menu and a module drop down menu:
$courseactive = 1;
$sql = "SELECT CourseId, CourseNo, CourseName FROM Course WHERE CourseActive = ? ORDER BY CourseNo";
$sqlstmt=$mysqli->prepare($sql);
$sqlstmt->bind_param("i",$courseactive);
$sqlstmt->execute();
$sqlstmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName);
$courses = array(); // easier if you don't use generic names for data
$courseHTML = "";
$courseHTML .= '<select name="courses" id="coursesDrop" onchange="getModules();">'.PHP_EOL;
$courseHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$outputcourse = "";
while($sqlstmt->fetch())
{
$course = $dbCourseId;
$courseno = $dbCourseNo;
$coursename = $dbCourseName;
$courseHTML .= "<option value='".$course."'>" . $courseno . " - " . $coursename . "</option>".PHP_EOL;
if (isset($_POST['courses']) && ($_POST['courses'] == $course)) {
$outputcourse = "<p><strong>Course:</strong> " . $courseno . " - " . $coursename . "</p>";
}
}
$courseHTML .= '</select>';
$moduleHTML = "";
$moduleHTML .= '<select name="modules" id="modulesDrop">'.PHP_EOL;
$moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$moduleHTML .= '</select>';
$pHTML = "";
?>
<script type="text/javascript">
function getModules() {
var course = jQuery("#coursesDrop").val();
jQuery('#modulesDrop').empty();
jQuery('#modulesDrop').html('<option value="">Please Select</option>');
jQuery.ajax({
type: "post",
url: "module.php",
data: { course:course },
success: function(response){
jQuery('#modulesDrop').append(response);
}
});
}
</script>
What happens in the above code is that when the user selects a Course from the course drop down menu, it will navigate to the module.php page where in that script it will perform a query and output the modules which belongs to that course in the module drop down menu on this page.
But now I am thinking of not including the course drop down menu at all as it is not really needed. So I only have a module drop down menu. I still want the modules to be displayed by still navigating to the module.php script to display the modules option though.
So my question is that what do I need to change in the jquery code so that when I open up the above script, it will perform the jquery straight away and display the list of modules in the drop down menu (without relying on the Course drop down menu as that drop down menu will be removed)?
I am assuming that the jquery will need to go into a document.ready() function?
UPDATE:
<script type="text/javascript">
$(document).ready( function(){
function getModules() {
jQuery.ajax({
type: "post",
url: "module.php",
success: function(response){
jQuery('#modulesDrop').append(response);
}
});
});
}
</script>
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();
?>`
There are a few different ways you can execute a script on pageload:
You can do this with an inline javascript method on your body tag:
<body onload="method()">You can do this with jQuery:
$(document).ready({method();
});
You can do this in your javascript anytime after you declare the function:
Just call
method();anytime after you declare the function in your javascript.