-4
  1. subjects
  2. course
  3. chapters

I want to add 2 dynamic dropdown lists, one is for subjects, and one is for course. When I select subject, courses which is added to that subject should be loaded in the course dropdown list, and then add chapters to that courses.

How do I do that?

Any help would be appreciated.

Here is my code:

<div class="content-form-inner">

        <div id="page-heading">Enter the details</div>
        <div class="form_loader" id="thisFormLoader"></div>
        <div id="error_message"></div>
        <form name="thisForm" id="thisForm" action="editchapters.php?mode=<?php echo $_REQUEST['mode']; ?>&id=<?php echo $id; ?>" method="post" enctype="multipart/form-data">
            <table border="0" cellpadding="0" cellspacing="0" id="id-form" >
                <tr>
                        <th valign="top" style="width:0%"><span class="required">*</span>Subject</th>
                        <td style="width: 0%">
                        <select name="subject_name" class="select-form required " style="color:#000 !important;width:200px !important">
                        <option value="">Select</option>
                        <?php
                            $sql = "select * from mock_subject ";
                            $res = mysqli_query($dbhandle,$sql);
                            $numrows =mysqli_num_rows($res);
                            echo mysql_error();

                            if($numrows){

                                while($obj = mysqli_fetch_object($res)){

                                    if($obj->status == 1){

                                        if($subject == $obj->id){
                                            echo '<option value="'.$obj->id.'" selected>'.($obj->subject_name).'</option>';
                                        }
                                        else{
                                            echo '<option value="'.$obj->id.'">'.($obj->subject_name).'</option>';  
                                        }
                                    }
                                }
                            }
                        ?>
                        </select>
                        </td>

                        <td style="width: 0%;">
                            <div id="subject_id-error" class="error-inner"></div>
                        </td>
                        <td></td>
                </tr>

                <tr>
                    <th valign="top" style="width:0%"><span class="required">*</span>Course</th>
                    <td style="width: 0%">
                        <select name="course_name" class="select-form required " style="color:#000 !important;width:200px !important">
                            <option value="">Select</option>
                            <?php
                                $sql = "select * from mock_course ";
                                $res = mysqli_query($dbhandle,$sql);
                                $numrows =mysqli_num_rows($res);
                                echo mysql_error();

                                if($numrows){

                                    while($obj = mysqli_fetch_object($res)){

                                        if($obj->status == 1){

                                            if($course == $obj->id){
                                                echo '<option value="'.$obj->id.'" selected>'.($obj->course_name).'</option>';
                                            }
                                            else{
                                                echo '<option value="'.$obj->id.'">'.($obj->course_name).'</option>';   
                                            }
                                        }
                                    }
                                }
                            ?>
                        </select>
                    </td>

                    <td style="width: 0%;">
                        <div id="course_id-error" class="error-inner"></div>
                    </td>
                    <td></td>
                </tr>

                <tr>
                    <th><span class="required">*</span>Chapter</th>
                    <td><input type="text" name="chapter_name" class="inp-form required" value="<?php echo $chapter;?>" style="color:#000 !important;"></td>
                    <td>
                    <div></div>
                    </td>
                </tr>

                 <tr>
                    <th>&nbsp;</th>
                    <td valign="top"><input type="submit" name="submit_button" value="<?php echo $mode=="edit"? "Update" : "Add" ?>" class="form-submit" />
                    <input type="reset" value="Reset" class="form-reset" />
                 </tr>
            </table>
        </form>
         <div class="clear"></div>
    </div>
Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
Sadia Naseeba
  • 211
  • 2
  • 13
  • 1
    You need to use ajax functionality for that without ajax you can not get the menu dynamic. – Mitul Jun 13 '15 at 05:12

2 Answers2

1

One possible solution would, if your data set is comparatively small. Load all the data on page load, and use jquery to get value of dropdown, and based on that formulate the values for the other drop downs. Second Solution would be to us AJAX and call data from your database for every action. and print using Angular. I've attached a sample code for that. This is my_script.js

var app = angular.module('myApp', []);
 app.controller('customersCtrl', function($scope, $http) {
    $(document).ready(function(){
    callSetTimeout();    
});

function callSetTimeout(){
    setTimeout(function(){
        update();
        callSetTimeout();
    },200);
}

 function update(){
    $http.get("http://localhost/WhatsOnYourMInd/db.php")
            .success(function (response) {$scope.names = response.records;});
                  }
 });

This is for db.php

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mind";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM data";
$result = $conn->query($sql);
$jsonString=array();
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        array_push($jsonString,array('id' =>$row['id'],'name' =>$row['name'],'message' =>$row['message']));

    }
    echo json_encode(array("records"=>$jsonString));
} else {
    echo "0 results";
}
$conn->close();
?>

This is for index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Whats on your Mind</title>
 <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

</head>
<body>
<ul id="ajax"></ul>

<div ng-app="myApp" ng-controller="customersCtrl">

    <ol>
        <li ng-repeat="x in names">
            {{ 'Id:'+x.id + ' Name:' + x.name +' Message:'+x.message}}
        </li>
    </ol>

</div>
<script src="my_script.js"></script>
</body>
</html>
1

Easiest way of creating dynamic dropdown is by using jquery inside the head tag.
Give onchange() event and guide the data to another page using jquery code, and then link 2 dropdowns. The code I did is like this, which I felt is the easiest way for dynamic dropdowns.

jquery which I included is

<script>
  function ajaxDrp(data){
    $.ajax({
      method: "POST",
      url: "chapterDropdown.php",
      data: { 
        id: data
      }
    }).success(function(data) {
      $('#selectCourse').empty();
      $('#selectCourse').append(data);
    });
  }
</script>

#selectCourse is the id I have given to the other dropdown which have to be in sync with the first dropdown.

The given url in the jquery is the path where data of first dropdown is getting collected. In my case, the code is like this:

<?php

  $id = $_REQUEST['id'];
  $query = "SELECT * FROM `your table name` WHERE subject_id = " . $id;
  $result = mysqli_query($dbhandle,$query);
  $count = 0;
  $option`enter code here` = '';
  while($row = mysqli_fetch_assoc($result)) {
    $option .= '<option
    value="'.$row['id'].'">'.$row['course_name'].'</option>';
  }
  echo $option;
?>
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
Sadia Naseeba
  • 211
  • 2
  • 13