troubleshooting Question

Second List box not Populating

Avatar of David Schure
David Schure asked on
PHP
3 Comments1 Solution17 ViewsLast Modified:
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>
<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>    
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>'; 
    } 
} 
?>
ASKER CERTIFIED SOLUTION
David H.H.Lee
Digital Marketing (Hospitality)
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros