While Ajax JQuery

0

I have the following code:

$.ajax({
    type:"post",
    url:"../connect/post/infoClienteTask.php",
    data:'infoClienteTask='+fantasy,                
    success:function(responseData){
        console.log(responseData);          
           $(".historic-cliente").html('<li class="list-group-item">'+responseData.Project+' - '+responseData.Delivery+'</li>');        
    }
});

That by clicking on a list of clients, it shows the whole history of the client, but with this code I can only find the first one of the search.

In PHP I have the following codes.

POST:

header( 'Content-Type: application/json; charset=UTF-8' );    

$selInfoClienteTasks= new Tasks();

if($_SERVER['REQUEST_METHOD']=='POST'){

    $fantasy = $_POST['infoClienteTask'];   
    $stInfoClienteTasks = $selInfoClienteTasks->selectInfoClienteTask($fantasy);

    echo json_encode($stInfoClienteTasks);

    exit();
}

SELECT:

public function selectInfoClienteTask($fantasy){
        try {           
            $stmt = $this->conn->prepare("SELECT tasks.*, DATE_FORMAT( tasks.Delivery , '%d/%m/%Y' ) as Delivery FROM Tasks WHERE CompanyFantasy = '$fantasy'");
            $stmt->execute();           
            return $stmt->fetch(PDO::FETCH_ASSOC);

        }catch (PDOException $exception){
            header("Location: ./error.php?err=Unable-to-find-info");
            echo 'Error: '.$excption->getMessage();
            return null;
        }
    }

I need to do a While / Foreach / For in this Ajax to continue searching until I find all the information, p>     

asked by anonymous 24.11.2016 / 12:04

2 answers

2

Make the following changes to your code:

Select:

public function selectInfoClienteTask($fantasy){
    try {           
        $stmt = $this->conn->prepare("SELECT tasks.*, DATE_FORMAT( tasks.Delivery , '%d/%m/%Y' ) as Delivery FROM Tasks WHERE CompanyFantasy = '$fantasy'");
        $stmt->execute();           
        $data = array();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $data[] = array('Project' => $row['Project'], 'Delivery' => $row['Delivery']);
        }

        return $data;

    }catch (PDOException $exception){
        header("Location: ./error.php?err=Unable-to-find-info");
        // Removi as linhas na sequencia pois se você faz o redirect nada na sequencia será executado
    }
}

Ajax:

$.ajax({
    type:"post",
    url:"../connect/post/infoClienteTask.php",
    data:'infoClienteTask='+fantasy,                
    success:function(responseData){
        $('.historic-cliente').html('');
        console.log(responseData);   
           $.each(responseData, function(i, item) {
               $(".historic-cliente").append('<li class="list-group-item">'+responseData[i].Project+' - '+responseData[i].Delivery+'</li>');   
           });​  
    }
});
    
24.11.2016 / 12:33
1

Access the index of your response through a for :

responseData[i].Project

foreach is also possible, you would need to see the content from response to set up a sample code for you. Use the code passed by the friend Sergio (console.log (JSON.stringify (responseData));) to get it.

    
24.11.2016 / 12:31