Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:26:13+00:00 2026-06-15T13:26:13+00:00

I have php/mysqli and jquery code below where it displays a course drop down

  • 0

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(); 


?>`
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:26:15+00:00Added an answer on June 15, 2026 at 1:26 pm

    There are a few different ways you can execute a script on pageload:

    1. You can do this with an inline javascript method on your body tag:

      <body onload="method()">

    2. You can do this with jQuery:

      $(document).ready({
      method();
      });

    3. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my code below I have 2 drop down menus. One is a Course
I have a jquery code below which is displayed in the editsessionadmin.php page. Now
I have an issue with this jquery code below. Please give me advice or
I have this jQuery code to add <select> elements in a table row with
I am working on a filter functionality using ajax/jquery and php/mysql.I have two sets
I have a legacy PHP/MySQL app that calls mysql_connect(). Tons of existing downstream code
Here my code, I need to insert into mysql database I have mysql,php,android 1)
I have this piece of php - mysql code: When a user tagged in
I have a drop down list (ddlAccount) which i can choose an item from
Im getting a row count from Mysql using php and i have a jquery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.