Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to populate a listview of jQueryMobile with contents from PHP side.

load_info.php file have the following code:

if(isset($_GET['type'])){
    switch($_GET['type']){
        case 'locale': 
            load_locale("localhost", "root", "", "testing");
            break;
    }   
}

function load_locale($host, $user, $pass, $db){
    $db = new mysqli($host, $user, $pass, $db);
    $db->set_charset("utf8");
    $query = $db->prepare("SELECT `desc` FROM locale");
    $query->execute();
    $query->bind_result($desc);
    $query->store_result();
    $rows = array();
    while($query->fetch()){
        $rows[] = array("value" => $desc);
    }

    $query->close();
    $db->close();

    return json_encode($rows);      
}

And in the index.php page, I have the following:

<div data-role="page" id="page-locale"> 
    <div data-role="header" data-theme="b" data-close-btn="right">
        <h1>Locale</h1>
    </div>
    <div data-role="content" id="content">  
        <ul id="listview_locale" data-role="listview" data-inset="true" data-filter="true" data-autodividers="true" data-filter-placeholder="Search..">

        </ul>
    </div>
</div>

<script type="text/javascript">
        $(document).on("pagebeforeshow", "#page-locale", function() {
            $(function(){
                var items = "";
                $.getJSON("load_info.php?type=locale",function(data){
                    $.each(data, function(index, item) 
                    {
                        items += "<li>" + item.value + "</li>";
                    });
                    $("#listview_locale").html(items); 
                    $("#listview_locale").trigger("change");
                    $("#listview_locale").trigger("refresh");
                });
            });
        });
</script>

Although, when I click the button to go to the page id #page-locale, the listview has nothing. I don't receive any error, but also it has no content.

What is wrong?

Edit: Solved. Instead of

$.getJSON("load_info.php?type=locale", function(data){

must be

$.getJSON("classes/load_info.php?type=locale", function(data){

instead of

load_locale("localhost", "root", "", "testing");

must be

echo load_locale("localhost", "root", "", "testing");
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Try this

your array is multi-dimension array so u have to convert the inner array too

  while($query->fetch()){
        $data = array("value" => $desc)   
        $rows[] = json_encode($data);
    }

EDIT

If u declare like this

echo load_locale("localhost", "root", "", "testing");

add exit;

code should be:

echo load_locale("localhost", "root", "", "testing"); 
exit;

if you give break..it will break the switch statement not stop the whole process

you can declare like this too

if(isset($_GET['type'])){
    switch($_GET['type']){
        case 'locale': 
            echo load_locale("localhost", "root", "", "testing");
            break;
    }  
    exit; 
}
share|improve this answer
 
See my edit. Thank you. –  user3065191 Dec 5 at 11:49
 
check my updated answer –  rynhe Dec 5 at 11:55
 
Good point. I will add the exit function. Thanks. –  user3065191 Dec 5 at 12:12
 
welcome !!!!! :) –  rynhe Dec 5 at 12:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.