Link to home
Start Free TrialLog in
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

Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

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


 
Avatar of David Schure
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
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial