Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Populate Drop-down List via Another Drop-down List

Featured Replies

Hello,

 

My query seems to be quite a simple one. The solution, however, I am finding great difficulty to find/understand.

 

This is what I currently have;

 

<select class="choice" id="fijournal" name="fijournal" required="required">
<option value="" disabled selected>Select journal...</option>
<?php
$clientid = $_SESSION['clientid'];
require_once('connection.php');
$query = "SELECT ClientID, JournalID, Title
FROM tbl_jv_journals
WHERE ClientID = $clientid
ORDER BY Title";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$journalid = $row['JournalID'];
echo "<option id='fijournal' name='fijournal' value='$journalid'>";
echo $row['Title'];
echo "</option>";
}
?>
</select>
What I need is to have a second Select drop-down list to be populated based on the choice made in this first one. I am fine with the php/mySQL side of things but I need help with the Javascript that is required to achieve this.
The second Select drop-down list will house all relevant Volumes for the selected Journal and then, via a third drop-down list, all relevant issues shall be present.
Upon conducting some online research (Including google searches and a lot of headaches) I just could not get my head around the few solutions I came across.
I would very much appreciate a nod in the right direction and an explanation on how the solution works.
Thank you for reading,
- MjA -

You could use an ajax request and create the select using php, then populate the response from the ajax request (the select) inside a div's html.

Similar to:


$.ajax({
type: "POST",
url: "create_select.php",
data: { select_data_one: "John" }
})
.done(function( response ) {
jQuery("#div").html(response);
});

 

 

  • Author

Thank you for your reply.

 

I have very little experience with JavaScript (Possibly because I find it most difficult to understand out of all the web languages). Would you be able to explain what an ajax request is and how to use it?

 

 

I have the following code which I need to run when a selection is made from the first drop-down list. This is in it's own PHP file called 'form_issue_getter.php'

 

<?php

session_start();
require_once('connection.php');

$choice = $_SESSION['fijournal'];

$query = "SELECT Volume
          FROM tbl_jv_issues
          WHERE JournalID = $choice
          ORDER BY Volume";

$result = mysql_query($query);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "<option style='color: black;' id='fijournal' name='fijournal'>";
    echo $row['Volume'];
    echo "</option>";
}

 

This is what I have on my main page, after what is posted in my first post;

<select class="choice2" id="fivolume" name="fivolume" required="required">
            <option style="color: grey" value="" disabled selected>Select journal first...</option>
        </select>

        <select class="choice3" id="fiissue" name="fiissue" required="required">
            <option style="color: grey" value="" disabled selected>Select volume first...</option>
        </select>

 

Would this setup work with your proposed AJAX solution or am I looking at this all from the wrong angle?

 

- MjA -

Edited by SkullBob

An ajax request will make a request to a server side script like a php file.
The php file will do whatever you code it to do.

With an ajax request you could take some user input, pass it to a server side script, do some validation/calculation with it and then return a response.

https://api.jquery.com/jQuery.ajax/

 

Good luck! :)

  • 4 weeks later...
  • Author

I forgot to update this post. Thanks to a fair amount of head scratching, headaches, and half working code, I managed to get my head around using ajax to accomplish this need, and many more. Thank you.

 

- MjA -

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.