phpajaxjsonjquery-mobilejquery-mobile-collapsible

Information is not appended and displayed in jquery Collapsible


I'm working on this topic again and I did not find any solution yet

a. Action that I did: I build a query using PDO to get the information from DB using PHP, after that I add the results into an associative array and encode it in order to send them using ajax. Finally, I used $.ajax to get the information and build the jquery collapsible

b. Issue: The information is not being appended inside the structure of the collapsible. The console.log of the data received by ajax did not displayed any information

Database app

Table: Bugs id,type,status,severity,system,browser,title,description,creation_date

Table: Projects id, projectname, status, description, start_date, due_date

The structure of the app is:

File: errorList.php

<?php
    // Import the connection to the database  
    require_once 'connection.php';

    // Get the instance
    $conn = dbConnect();

    // Build the query 
    $sql = " SELECT bg.bug_id, py.project, bg.type, bg.status, bg.severity, bg.system, bg.browser, bg.title, bg.description,bg.creation_date FROM bugs bg, projects py WHERE bg.projectid = py.id ";

    // Run the query and gets the results
    $result = $conn->query($sql)->fetchAll(PDO::FETCH_ASSOC);

    // Build an iteration and create an associative array   
    foreach ($result as $row){

    $return[]=array(
                'bug_id'=>$row['bug_id'],
                'project'=>$row['project'],
                'type'=>$row['type'],
                'status'=>$row['status'],
                'severity'=>$row['severity'],
                'system'=>$row['system'],
                'browser'=>$row['browser'],
                'title'=>$row['title'],
                'description' => $row['description'],
                'creation_date'=>$row['creation_date']);
     }

     // Encode the array 
     echo json_encode($return);

?>

File: index.html / Page: #bugs

Inside of the html file I added the div "SET"

<div data-role="collapsible-set" id="set"><div>
    <script type="text/javascript">

        $(document).on('pagebeforeshow', '#bugs', function (){

                $.ajax({
                    url: 'inc/errorList.php',
                    data: "",
                    dataType: 'json',
                    success: function(data)
                    {
                        // Create the variable content to concatenate the results
                        var content = "";

                        // Build an iteration of the information received in data

                        for (var i = 0; i < data.length; i++) {

                            // Add the header of the collapsible  
                            content = "<div data-role='collapsible' data-collapsed-icon='arrow-r'   data-expanded-icon='arrow-d' data-iconpos='right' data-theme='a'><h3> " + data[i].bug_id + ' - ' + data[i].title + "</h3>" + "<ul data-role='listview' data-inset='true'  data-theme='a'>";

                            // Concatenate the header with the information retreived
                             content = content +
                            '<li>Project: ' + data[i].project + '</li>' +
                            '<li>Type: ' + data[i].type + '</li>' +
                            '<li>Status: ' + data[i].status + '</li>' +
                            '<li>Severity: ' + data[i].severity + '</li>' +
                            '<li>Browser: ' + data[i].browser + '</li>' +
                            '<li>Creation Date ' + data[i].creation_date + '</li>' +
                            '<li>Detail: ' + data[i].description + '</li>' ;

                            content = content + '</ul>';
                            content = content + "</div>";

                            // Append the content into the div     
                            $("#set").append(content);
                        }

                        // Refresh the Collapsible 
                        $("#set").collapsibleset("refresh").enhanceWithin();
                       }
                    });
            });
    </script>  

Solution

  • Yes, Finally I found the bug!. The problem was the accents in some words in the field 'Description, below I add the final code:

    File: errorList.php

    <?php
    // Import the connection to the database  
    require_once 'connection.php';
    
    // Get the instance
    $conn = dbConnect();
    
    // Build the query 
    $sql = " SELECT bg.bug_id, py.project, bg.type, bg.status, bg.severity, bg.system, bg.browser, bg.title, bg.description,bg.creation_date FROM bugs bg, projects py WHERE bg.projectid = py.id ";
    
    // Run the query and gets the results
    $result = $conn->query($sql)->fetchAll(PDO::FETCH_ASSOC);
    
    // Build an iteration and create an associative array   
    foreach ($result as $row){
    
    $return[]=array(
                'bug_id'=>$row['bug_id'],
                'project'=>$row['project'],
                'type'=>$row['type'],
                'status'=>$row['status'],
                'severity'=>$row['severity'],
                'system'=>$row['system'],
                'browser'=>$row['browser'],
                'title'=>$row['title'],
                'description' => $row['description'],
                'creation_date'=>$row['creation_date']);
     }
    
    header("Content-type: application/json", true);
    echo json_encode($return);
    exit;
    
    ?>
    

    File: json.js

    address = 'inc/errorList.php';
    
    
    $.ajax({url: address,
            cache: false,
            data: "",
            dataType: 'json',
            success: function(new_data){
    
      // Create the variable content to concatenate the results
                        var content = "";
    
                        // Build an iteration of the information received in data
    
                        for (var i = 0; i < data.length; i++) {
    
                            // Add the header of the collapsible  
                            content = "<div data-role='collapsible' data-collapsed-icon='arrow-r'   data-expanded-icon='arrow-d' data-iconpos='right' data-theme='a'><h3> " + data[i].bug_id + ' - ' + data[i].title + "</h3>" + "<ul data-role='listview' data-inset='true'  data-theme='a'>";
    
                            // Concatenate the header with the information retreived
                             content = content +
                            '<li>Project: ' + data[i].project + '</li>' +
                            '<li>Type: ' + data[i].type + '</li>' +
                            '<li>Status: ' + data[i].status + '</li>' +
                            '<li>Severity: ' + data[i].severity + '</li>' +
                            '<li>Browser: ' + data[i].browser + '</li>' +
                            '<li>Creation Date ' + data[i].creation_date + '</li>' +
                            '<li>Detail: ' + data[i].description + '</li>' ;
    
                            content = content + '</ul>';
                            content = content + "</div>";
    
                            // Append the content into the div     
                            $("#set").append(content);
                        }
    
                        // Refresh the Collapsible 
                        $("#set").collapsibleset("refresh").enhanceWithin();
                       },
            error: function (request,error) {
    
                //var err = eval("(" + xhr.responseText + ")");
                //alert(err.Message);
                // This callback function will trigger on unsuccessful action                
                alert('Connection Error!');
                alert('Error: ' + error);
                //console.log(new_data);
    
            }
    
        });           
    
     });