信息未附加并显示在jquery Collapsible中

时间:2014-11-08 19:50:51

标签: php ajax json jquery-mobile jquery-mobile-collapsible

我正在再次研究这个话题,但我还没有找到任何解决方案

一个。我做的动作:我使用PDO构建一个查询,使用PHP从DB获取信息,之后我将结果添加到关联数组中并对其进行编码,以便使用ajax发送它们。最后,我使用$ .ajax获取信息并构建jquery可折叠

湾问题:信息未附加在可折叠结构内。 ajax收到的数据的console.log没有显示任何信息

数据库应用

表:错误 id,type,status,severity,system,browser,title,description,creation_date

表:项目 id,projectname,status,description,start_date,due_date

应用程序的结构是:

  • INC / errorList.php
  • INC / connection.php
  • index.html(多页文件)

文件: 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);

?>

文件: index.html /页面: #bugs

在html文件中我添加了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>  

1 个答案:

答案 0 :(得分:0)

是的,最后我发现了这个错误!问题是该字段中某些词语的重音&#39;描述,下面我添加了最终代码:

文件: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;

?>

档案: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);

        }

    });           

 });
相关问题