-3

I'm making a online system for taking test as you would be taking in school. You'r student account would be bound to a class and your teachers would give you tests to complete for a grade or to practice.

At this point I'm in the stage where the teacher chooses his class or subject (Math, English, History etc.) in one select box. Then once they choose their subject they would be presented with a new select option box to select a field (Algebra, Trigonometry etc.).

My problem at this point is that once they choose a subject, Math for example, I want the new select box to (if possible) pull data form my database and check for the fields that are associated to that subject. So for math they would get option to choose algebra or trigonometry or other.

I'm not looking for complete code but snippets of code and explanation of that code would go a long way.

Currently i am pretty well familiar with PHP, HTML and CSS. I am currently learning how to use JS in my website.

What I came up: Script that is used to get the choice

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    $("#subjectSelect").change(function() {
    $("#fieldSelect").load("getter.php?choice=" + $("#subjectSelect").val());
    });
</script>

Getter.php

<?php include "includes/dbc.inc.php"?>
<?php session_start();?>
<?php
$choice = mysql_real_escape_string($_GET['choice']);
$query = "SELECT * FROM fielda WHERE field_subj_id='$choice'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
    echo '<option value="'.$row['field_id'].'">' . $row['field_name']} . '</option>';
}

Website Code:

case "fieldquesadd":
            echo '
                <fieldset align="left">
                    <legend>Unos pitanja</legend>
                    <p>
                        <label for="subject">
                            <span>Predmet:  </span>
                        </label>
                        <select name="subject" required id="subjectSelect">
                            <option value=""></option>
                        ';
            $querySubjects = "SELECT * FROM subject";
            $resultSubjects = mysqli_query($conn, $querySubjects);
            while ($row_subjects = mysqli_fetch_array($resultSubjects)) {
                echo '<option value="' . $row_subjects['subj_id'] . '">' . $row_subjects['subj_name'] . '</option>';
            }

            echo '
                        </select>
                    </p>
                    <p id="fieldSet">
                        <label for="fieldSet">
                            <span>Gradivo: </span>
                        </label>
                        <select name="fieldSet" id="fieldSelect" required>

                        </select>
                    </p>
                </fieldset>
            ';
            break; //case "fieldquesadd"
mike6715b
  • 69
  • 10
  • 2
    Please show some attempt that you have made to solve this and describe what problem(s) you are having in detail. – Patrick Q May 14 '18 at 17:39
  • The problem is that i guess i dont know how to get the data from MySQL database when the user selects an option. I've tried using 2 (PHP) while loops inside of each other that get the values for all fields and then output the data depending on what the user chooses with javascript. The fact is im not that skilled in JS to handle this and its my personal project and i intend to learn so i thought i could seek advice here. – mike6715b May 14 '18 at 17:44
  • 1
    Like I said, please show your attempt. We can't help you if we don't know where you're having trouble. There are many _many_ examples of doing this. Some basic searching should give you at least a starting point. – Patrick Q May 14 '18 at 17:46
  • I have tried to simply post the first choice to a new website and then there the user chooses the field but im now looking for a way to do it all on the same website. I'm not quite sure where to begin. – mike6715b May 14 '18 at 17:48
  • 1
    If you're even vaguely serious, start over. PHP's mysql_ API was deprecated a long time ago. – Strawberry May 14 '18 at 20:30
  • Im not sure what your saying but i already am using php and mysql to interface with the database and the website. Just need to know how could i use JS to interface with mysql and siplay data on the website if a certain option is selected in option subject – mike6715b May 15 '18 at 04:58

1 Answers1

1

The thing i can suggest is to make the new select box to display:none; until something is selected and you can make it display using js when selected

selector.style.display="block";

refer to How to display div after click the button in Javascript?

to get value from select box

refer to

Get selected option text with JavaScript

Retrive value from that select box using php $_POST['name'] method

perform a SQL query to create values in new select box same as you do to display content in table using sql and php.

or refer to Display Mysql table field values in Select box

  • i did think of just simply making the user choose a class and the be moved to a new website to choose a field but I was hopping of doing it all on the same page. – mike6715b May 14 '18 at 17:46
  • i have given links to all the problems you mentioned in the question, i suggest you to go through these links i think that your problem can be resolved by these solutions – Amitoj Singh Ahuja May 14 '18 at 17:49
  • I am aware of the thing the website in the links say but im looking to make JS to connect to the database maybe and ,depending on the choice of the user, display the corresponding information to the user after he clicks the button. – mike6715b May 14 '18 at 17:52
  • ajax can be used for this – Amitoj Singh Ahuja May 14 '18 at 17:55
  • May I ask how can I do that? – mike6715b May 14 '18 at 17:57
  • Actually let me confirm that what actually you require ,what i understood is that you want to display the data according to the user input in select box and display options which are related to the previous choice.I am not getting the point of doing it one page. – Amitoj Singh Ahuja May 14 '18 at 18:06
  • I believe it would be simpler then going to 3 seperate websites. I intend to have other options displayed after the fact but only the 1st option wil dictate what options will be availble in the 2nd option box – mike6715b May 14 '18 at 18:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171024/discussion-between-amitoj-singh-and-mike6715b). – Amitoj Singh Ahuja May 14 '18 at 18:16