简体   繁体   中英

How do i get record in a dropdown list based on a first drop down list in php ajax request

Php ajax request not working even though the onchange function seems to be working.

Initially the get_data.php was in a folder different from the parent folder. I moved it to the same folder but still not working.

//html select option
<select name="slct" id="slct1">
                <option selected disabled>----Choose Class ----</option>
              <?php
       $query = "SELECT * FROM classes ";

//GET RESULT
       $result = $pdo->query($query);
       $result->setFetchMode(PDO::FETCH_ASSOC);
       while($row = $result->fetch()){
        ?>
           <option value="<?php echo $row['class_name']?>"><?php echo $row['class_name']?></option>
           <?php
         }
         ?>
   <select name="slct" id="slct2" >
              <option selected disabled>----Select Student----</option>

            </select>


//ajax request
<script type="text/javascript"> 
$(document).ready(function(){
      $('#slct1').on('change',function(){
        var StudentID = $(this).val();
        if(StudentID){
            $.ajax({
                type:'POST',
                url:'get_data.php',
                data:'class_name='+StudentID,
                success:function(html){
                    $('#slct2').html(html);
                }
            }); 
        }else{
            $('#slct2').html('<option value="">Select Class First</option>'); 
        }
    });
});
</script>



//get_data.php
<?php
//Include the database configuration file
include '../config/dbconfig.php';
if(isset($_POST["class_name"])){
    //Fetch all state data
    $query = "SELECT * FROM students WHERE class = ".$_POST['class_name']." ORDER BY id ASC";

    //Count total number of rows
    $result = $pdo->query($query);
    $result->setFetchMode(PDO::FETCH_ASSOC);
    $rowCount = $result->num_rows;

    //State option list
    if($rowCount > 0){
        echo '<option value="">Select Student</option>';
        while($row = $result->fetch()){ 
            echo '<option value="'.$row['class'].'">'.$row['firstname']. $row['middlename'] . $row['lastname']. '</option>';
        }
    }else{
        echo '<option value="">No Student Registered</option>';
    }
}

?>

I want to get student records in the second select option when class is selected.

As mentioned in the comment - it looks like the issue might be dues to lack of quotes around the POST variable in the SQL query. This would not have been an issue had you used a prepared statement and bound the POST variable to a placeholder - the added benefit is of course greater protection from SQL injection.

<?php

    if( isset( $_POST['class_name'] ) ){

        include '../config/dbconfig.php';


        $sql = 'select * from `students` where `class` = :classname order by id asc';
        $args = array( 
            ':classname' => $_POST['class_name']
        );


        $stmt=$pdo->prepare( $sql );
        if( $stmt ){

            $result=$stmt->execute( $args );

            if( $result && $stmt->rowCount() > 0 ){

                echo '<option selected hidden disabled>Select student';
                while( $rs=$stmt->fetch( PDO::FETCH_OBJ ) ){
                    printf('<option value='%s'>%s %s %s', $rs->class, $rs->firstname, $rs->middlename, $rs->lastname );
                }
            } else {
                echo '<option selected hidden disabled>No student registered';
            }
        } else {
            exit( '<option selected hidden disabled>error' );
        }
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM