DevGe
DevGe

Reputation: 1449

How to get the response data throught looping in my backend page

I have question, how to select all the data through looping in my backend page? then in my front end i want to use the each function of jquery to append all the data in the corresponding class. I already create a function and the status is 200 in which is correct right? so when i click the response callback.js:31 they point me in the console.log error why is it happen my status is 200? to better understand i will share to you guys my code.

callback.js:31 {readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}

Main point question: How to get the data through looping in my jquery.

Ajax:

$.ajax({
    url:'./Function/fetch_mission_vision.php',
    type:'get',
    success:function(response) {
        $.each(response,function(i,el){
            console.log(el.title);
        });

    },
    error:function(error) {
        console.log(error);
    }
})

Php:

    <?php 
    require_once('../ConnectionString/require_db.php');
    session_start();
    //submit function
    $status = 'Active';
    $posttype_mission = 'mission';
    $posttype_vision = 'vision';
    $about_psmid_content = $db->prepare('SELECT title,content FROM tblcontent 
    WHERE status = ? AND posttype = ? OR posttype = ? ORDER BY contentID DESC LIMIT 2') 
    or die($db->error);

    $about_psmid_content->bind_param('sss',$status,$posttype_mission,$posttype_vision);
    $about_psmid_content->execute();
    $about_psmid_content->bind_result($title,$content);

    while ($about_psmid_content->fetch()) {
        $data = ['title'=>$title,'content'=>$content];
       header('Content-type: application/json');
       echo json_encode($data);
   }



?>

Upvotes: 0

Views: 38

Answers (1)

William J.
William J.

Reputation: 1584

What about building an array with all your data and then json encoding it after the loop?

  while ($about_psmid_content->fetch()) {
     $data[] = ['title' => $title,'content' => $content];
  }
  header('Content-type: application/json');
  echo json_encode($data);

Upvotes: 1

Related Questions