0

I am using wampserver and its php, I have coursetable with 2 primary keys, these are subject and course. Subjects can be like math, cs (computer science) etc. Courses can be like 306, 307 etc, but both of them are varchar. I want to create 3 drop down list which 2 of them are linked. For example, if I select cs in first drop down, second one can be 300, 306 or 307, but if I select math subject in first dropdown list courses can be 203 or 306. The third dropdown list is grades and same in everywhere. The problem is I cannot get subject and course data like https://www.youtube.com/watch?v=8UMCg2KQcsM video or Dynamic dropdown list link. Can anyone help me to solve that problem.

connection.php

<?php
$con = mysqli_connect('localhost', 'root', '123456', 'inputdatabase');
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect:".mysqli_connect_errno();
} ?>

getdata.php

<?php
include_once "connection.php"

if(!empty($_POST["course"])){
  $course = $_POST["course"];
  $query = "SELECT * FROM coursetable WHERE subject = $course";
  $result = mysqli_query($con, $query);

  foreach($result as $coursetable){
  ?>
  <option value="<?php echo $coursetable["course"]; ?>"></option>
  <?php
  }
}?>

one part of html - I added connection.php, it is not problem

                <select name="subject" onchange="getId(this.value);">
                    <option value="">Select course</option>
                        <?php
                        $query = "SELECT * FROM coursetable";
                        $results = mysqli_query($con, $query);

                        foreach ($results as  $coursetable){
                        ?>
                        <option value="<?php echo $coursetable["subject"];?>"></option>
                        <?php
                            }
                        ?>
                </select>

                <div class ="course">
                  <select name="course" id="courseList">
                    <option value=""></option>
                  </select>
                </div>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
           </script>
                <script>
                  function getId(val){
                      $.ajax({
                        type: "POST",
                        url: "getdata.php",
                        data : { course : val },
                        success: function(data){
                          $("#courseList").html(data);
                        }
                      })
                  }
                </script>
ozan
  • 33
  • 1
  • 9
  • 1st: you need to use `data : { course : val },` instead of `data: "course="+val, `.. 2nd: you need to show us the errors you got *console.log(data) on ajax success function* – Mohamed-Yousef Nov 19 '17 at 18:37
  • thank you. I wrote first one. I cannot show error. The dropdown list shows 2 things select course and "> . "> means I have error in DOM but I dont. I think it is php error or table/column name error or sth. – ozan Nov 19 '17 at 18:41
  • Try in php use `while($coursetable = mysqli_fetch_assoc($results))` instead of `foreach ($results as $coursetable)` – Mohamed-Yousef Nov 19 '17 at 18:56
  • I tried that one as well. I make my code better but it is still not working. – ozan Nov 19 '17 at 18:59
  • your code is simple .. but be sure to check for errors on every single step while coding .. don't go to ajax part without checking the php part is already done without any errors then go to the ajax part .. my previous comments is what I always use in my projects and it works very well .. so please use my previous codes .. Finally maybe you need to use single quotes `subject = '$course'` instead of `subject = $course` .. Good Luck :) – Mohamed-Yousef Nov 19 '17 at 19:11
  • I tried. Not enough for working. Thank you for your help. I have never work for AJAX, jquery and php. I worked on C++ too much. Therefore, I confusion. – ozan Nov 19 '17 at 19:21
  • Ok from your code you have `inputdatabase` database name and inside it you've a table `coursetable` with columns names `subject` and `course` is that right?? – Mohamed-Yousef Nov 19 '17 at 19:26
  • Yes and both of them are primary keys – ozan Nov 19 '17 at 19:27
  • from [this answer](https://stackoverflow.com/a/217952/3385827) its no problem to have more primary keys .. but Actually after my codes I posted to you I can't find anything can make your code not working .. I'm really sorry – Mohamed-Yousef Nov 19 '17 at 19:37
  • I understand if I use 2 primary keys, I cannot write any code which makes this type of 2 linked dropdown list. Is it correct? – ozan Nov 19 '17 at 19:40
  • I'm not expert in sql but I think it'll not affect your code .. but if you've any doubt It'll be better to check the query inside phpmyadmin – Mohamed-Yousef Nov 19 '17 at 19:51
  • I sure it is not problem – ozan Nov 19 '17 at 19:53
  • Now I make some changes and it is too close to solve. However, I get error. It tells --- ( ! ) Notice: Use of undefined constant subject - assumed 'subject' in C:\wamp\www\MLWebsite\website\index.php on line 146 – ozan Nov 19 '17 at 20:43

0 Answers0