0

I have created a drop down menu in php that is displayed however, when a value has been clicked, I don't know how to collect this information.

<html>
<body>
<?php
$mydb = new mysqli('localhost','root','','TestTimeTableSolution');
$rows = $mydb->query("SELECT DISTINCT TeacherID FROM Teacher;")->fetch_all(MYSQLI_ASSOC);
$teachers = array();

foreach ($rows as $row) {
    array_push($teachers, $row["TeacherID"]);
}

$dropdownMenu = "<select name='TeacherID' form='Teacher'><option value='Null' selected>All</option>";
foreach ($teachers as $topic) {
    $dropdownMenu .= "<option value='" . $topic . "'>" . $topic . "</option>";
}
$dropdownMenu .= "</select>";

echo $dropdownMenu;
?>
</body>
</html>
5
  • "how to collect this information" - where you wanna collect that info? in javascript? in the next php script after a form submit? Commented Jan 25, 2018 at 17:36
  • I want to collect the name that is clicked on as a string Commented Jan 25, 2018 at 17:39
  • yeah, but where Commented Jan 25, 2018 at 17:43
  • i don't know what I need to write or where i need to put it to collect what is clicked on Commented Jan 25, 2018 at 17:47
  • i want it to be dynamic so as soon as the user clicks on something the relevant information will pop up Commented Jan 25, 2018 at 18:00

1 Answer 1

1

Based on your last comment, "i want it to be dynamic so as soon as the user clicks on something the relevant information will pop up", it sounds like you will probably want to use Ajax/JavaScript (I will demonstrate a simple jQuery example, notating for clarity):

<?php
$mydb = new mysqli('localhost','root','','TestTimeTableSolution');
?>
<html>
<!-- Add the jQuery library -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
// Act when the document is ready
$(function(){
    // listen for the select to change
    $('select[name=TeacherID]').on('change',function(){
        // Run the ajax – you can also use the shortcut $.post method found at:
        // https://api.jquery.com/jquery.post/
        $.ajax({
            // This is the page that is going to do the data lookup/display action
            url: '/lookup.php',
            // This is how it's sending the data to that page
            type: 'post',
            // This is what is being sent ($_POST['submit'] in this case)
            data: {
                // Use $(this) to isolate the current selected element and get value (.val())
                // The value is represented as $topic in your php
                'submit': $(this).val()
            },
            // If all goes well and the page (lookup.php) returns a response
            success: function(response) {
                // Place the response into the div (see html snippet)
                $('#loadspot').text(response);
            }
        });
    });
});
</script>
<body>
<?php
$rows = $mydb->query("SELECT DISTINCT TeacherID FROM Teacher;")->fetch_all(MYSQLI_ASSOC);
$teachers = array();
foreach ($rows as $row) {
    array_push($teachers, $row["TeacherID"]);
}

$dropdownMenu = "<select name='TeacherID' form='Teacher'><option value='Null' selected>All</option>";
foreach ($teachers as $topic) {
    $dropdownMenu .= "<option value='" . $topic . "'>" . $topic . "</option>";
}
$dropdownMenu .= "</select>";

echo $dropdownMenu;
?>
<!---------------------------------------------->
<!-- THIS IS WHERE THE CONTENT WILL LOAD INTO -->
<!---------------------------------------------->
<div id="loadspot"></div>
</body>
</html>

In order for this to work, you need the page lookup.php in the domain root (you can make it whatever/where ever you want, but you need to match in the javascript url):

/lookup.php

<?php
# This is what will get placed into the parent page <div id="loadspot"></div>
# Do you your php here in place of this line and return whatever "relative information" you want
print_r($_POST);

You should review the jQuery page I have linked to to get more information and direction for that library and make sure you use your browser's developer tools to monitor javascript errors in the console. Ideally, you want to understand all this via the documentation instead of just copy and paste and move on...

Sign up to request clarification or add additional context in comments.

14 Comments

where and what do i need to add, so that the item clicked on will become the contece of a string?
Where are you going to use the string? You have to know how/where in order to tell you the best way to get it,.
I want to store it as a string so I can then query the database, using it as a condition
Ok so you have the dropdown and option might be <option value="1234">John Doe</option> and you are wanting to get 1234?
Anyway, if that is the case, then what I have is an example of that, processed through ajax. The database lookup would be done on the lookup.php page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.