Avatar of David Schure
David Schure
 asked on

Second List box not Populating

https://arise.plus/ADMIN/therapist-client-report.php
<!-- Therapist dropdown -->
<?php
$result = mysqli_query($con, "SELECT tbl_therapist.therapist_id, tbl_therapist.therapist_name FROM tbl_therapist ORDER BY therapist_name ASC;");
?>
<label><strong>THERAPIST</strong></label><BR>
<select name="therapist" id="therapist" size="10">
<?php
while ($row = mysqli_fetch_array($result)) {
    printf("<option value='%s'>%s</option>", $row['therapist_id'], $row['therapist_name']);
}
?>
</select>

<!-- Client dropdown -->
<select id="client">
    <option value="">Select Therapist first</option>
</select>

<!-- Session dropdown -->
<select id="session">
    <option value="">Select Client first</option>
</select>

Open in new window

<script>
$(document).ready(function(){
    $('#therapist').on('change', function(){
        var therapistID = $(this).val();
        if(therapistID){
            $.ajax({
                type:'POST',
                url:'ajaxData.php',
                data:'therapist_id='+therapistID,
                success:function(html){
                    $('#client').html(html);
                    $('#session').html('<option value="">Select Therapist first</option>'); 
                }
            }); 
        }else{
            $('#client').html('<option value="">Select Therapist first</option>');
            $('#session').html('<option value="">Select Client first</option>'); 
        }
    });
    
    $('#therapist').on('change', function(){
        var therapistID = $(this).val();
        if(therapistID){
            $.ajax({
                type:'POST',
                url:'ajaxData.php',
                data:'therapist_id='+clientID,
                success:function(html){
                    $('#session').html(html);
                }
            }); 
        }else{
            $('#session').html('<option value="">Select Client first</option>'); 
        }
    });
});
</script>    

Open in new window

ajaxData.php page
<?php 
// Include the database config file 
include_once 'includes/pdo_connection.php'; 
 
if(!empty($_POST["therapist_id"])){ 
    // Fetch state data based on the specific country 
    $query = "SELECT tbl_client.client_name
FROM tbl_client
INNER JOIN tbl_therapist_client
ON tbl_therapist_client.client_id=tbl_client.client_id
WHERE tbl_therapist_client.therapist_id = ".$_POST['therapist_id']."
ORDER BY tbl_client.client_name ASC";

    $result = $db->query($query); 
     
    // Generate HTML of state options list 
    if($result->num_rows > 0){ 
        echo '<option value="">Select Client</option>'; 
        while($row = $result->fetch_assoc()){  
            echo '<option value="'.$row['client_id'].'">'.$row['client_name'].'</option>'; 
        } 
    }else{ 
        echo '<option value="">Client not available</option>'; 
    } 
}elseif(!empty($_POST["client_id"])){ 
    // Fetch city data based on the specific state 
    $query = "SELECT * FROM tbl_session WHERE client_id = ".$_POST['client_id']."  ORDER BY client_name ASC"; 
    $result = $db->query($query); 
     
    // Generate HTML of city options list 
    if($result->num_rows > 0){ 
        echo '<option value="">Select Session</option>'; 
        while($row = $result->fetch_assoc()){  
            echo '<option value="'.$row['session_id'].'">'.$row['session_date'].'</option>'; 
        } 
    }else{ 
        echo '<option value="">Session not available</option>'; 
    } 
} 
?>

Open in new window

PHP

Avatar of undefined
Last Comment
David H.H.Lee

8/22/2022 - Mon
David H.H.Lee

I didn't see your client_id field in your query?

$query = "SELECT tbl_client.client_name FROM tbl_client INNER JOIN ..."

change to

$query = "SELECT tbl_client.client_id,tbl_client.client_name FROM tbl_client INNER JOIN ..

Open in new window


 
David Schure

ASKER
Good catch!  I added the client_id but still no populated listbox.  I ran the query in Navicat and it returns value...
"SELECT tbl_client.client_id,tbl_client.client_name
FROM tbl_client
INNER JOIN tbl_therapist_client
ON tbl_therapist_client.client_id=tbl_client.client_id
WHERE tbl_therapist_client.therapist_id = ".$_POST['therapist_id']."
ORDER BY tbl_client.client_name ASC";

Open in new window

ASKER CERTIFIED SOLUTION
David H.H.Lee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes