While Ajax Jquery

Asked

Viewed 236 times

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 customer list, it shows all the customer history, but with this code I can only find the first one in 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 make a While/Foreach/For in this Ajax to continue searching until you find all the information, someone has an idea?

  • What gives this one console.log(responseData);? or better: console.log(JSON.stringify(responseData));

  • All corresponding data from the client table in question, but there are two tasks for the same client, but only the first one appears.

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>');   
           });​  
    }
});
  • With the PHP part appeared the two data of the same client, but at the time of printing, it only pays the last data entered.

  • Sorry, forgot to change to use the correct method, I fixed now!

  • I made a little change $('.historic-cliente').html('');, before the $.each, to clean, otherwise it will always add.

  • Perfect, I added too!

1

Access your Sponse index via a for:

responseData[i].Project

The foreach also possible, you would need to see what content from Answer to mount an example code for you. Use the code passed by your friend Sergio (console.log(JSON.stringify(responseData));) to get it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.