Click here to Skip to main content
15,885,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please am having problem with my code.... it's not returning data from database.


XML
<script type="text/javascript">
var myTimer;

function ajax_json_data(){
    var databox = document.getElementById("databox");
    var arbitrarybox = document.getElementById("arbitrarybox");
    var hr = new XMLHttpRequest();
    hr.open("POST", "request.php", true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var d = JSON.parse(hr.responseText);
            arbitrarybox.innerHTML = d.arbitrary.returntime;
            databox.innerHTML = "";
            for(var o in d){
                if(d[o].title){
                    databox.innerHTML += '<p><a href="page.php?id='+d[o].id+'">'+d[o].title+'</a><br>';
                    databox.innerHTML += ''+d[o].cd+'</p>';
                }
            }

        }

    }
   hr.send("limit=");
    databox.innerHTML = "requesting...";
    myTimer = setTimeout('ajax_json_data()',6000);
}
</script>



PHP
<?php
header("Content-Type: application/json");
if(isset($_POST['limit'])){
    $limit = preg_replace('#[^0-9]#', '', $_POST['limit']);
    include_once("phpincludes/dataconect.php");
    $i = 0;
    $jsonData = '{';
    $sqlString = "SELECT * FROM bible WHERE  ORDER BY RAND() LIMIT $limit";
    $query = mysqli_query($db_conx, $sqlString) or die (mysqli_error());
    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
        $i++;
        $id = $row["id"];
        $title = $row["title"];
        $cd  = $row["creationdate"];
        $cd = strftime("%B %d, %Y", strtotime($cd));
        $jsonData .= '"article'.$i.'":{ "id":"'.$id.'","title":"'.$title.'", "cd":"'.$cd.'" },';
    }
    $now = getdate();
    $timestamp = $now[0];
    $jsonData .= '"arbitrary":{"itemcount":'.$i.', "returntime":"'.$timestamp.'"}';
    $jsonData .= '}';
    echo $jsonData;
    }else
        echo ("nothing was posted");

?>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900