SweetAlert Select library issue

43 Views Asked by At

I am trying to allow the user to search from the dropdown list if the list are too long. I am using sweetalert select library but not working for me

This is my HTML code

<div class="form-group col-md-6">
                            <label>Religion:</label>
                                <select class="form-control" id="updateReligion" name="updateReligion" required></select>
                             

This is my function code

function populateReligionDropdown() {
        $.ajax ({
            url: 'data/fn_getData_admin.php?mod=workerReligion',
            type: 'GET',
            dataType: 'json',
            async: false,
            success: function(response) {
                //Assuming the response is an array of objects with 'id' and 'itemname' properties
                var religionDropdown = $('#updateReligion');
                religionDropdown.empty(); //Clear existing options
                religionDropdown.append("<option></option>");

                console.log(response.workerReligion);

                $.each(response.workerReligion, function(index, item) {
                    religionDropdown.append(`<option value="${item.religion}">${item.religion}</option>`);
                });

                $('#updateReligion').select2();
                
            },
            error: function() {
                //Handle AJAX error
                Swal.fire('Error occurred while fetching religion data. Please try again');
            }
        });
    }

This is fn_getData_admin.php code

<?php

session_start();
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
// Require database connection file
require_once '../includes/db_connect_admin.php';

// Include libFunction
include_once '../includes/libFunction.php';

$libFunction = new LibFunction();

// Settings for MeekroDB
DB::$error_handler = false; // since we're catching errors, don't need error handler
DB::$throw_exception_on_error = true;// connecting to db

$module = $_GET['mod'];

$workerID = NULL;



$result = array();
$result['status'] = TRUE;
$result['msg'] = 'Retrieve Data Successful';
$result['custMsg'] = '';

try {
    //code...
    switch ($module) {

        case 'workerReligion':
            $sql = "SELECT a.religion 
                    FROM dm_religion a 
                    WHERE a.in_use = 1 
                    ORDER BY a.religion ASC";

            $rs = DB::query($sql);
            
            break;
            
            
 }


} catch (MeekroDBException $e) {
        $result['status'] = FALSE;
        $result['msg'] = "Failed to retrieve data.";
        $result['custMsg'] = $e->getMessage();
        $result['query'] = $e->getQuery();
    }
    
    
    if (empty($rs)) {
        $rs = [];
    }
    $result[$module] = $rs;
    
    echo json_encode($result);
    
    ?>

The list are not appearing and I am populating the data dynamically. The select2 sweetalert library are already imported

The data to be appear under the dropdownlist and user are able to search from the list

0

There are 0 best solutions below