1

Hello I'd like to send controller data to view with json but I'm a blocked.

My model (contrat_model) :

function presence($id_personnel,$debut,$fin){    
        $presence='';    
        $query="select statut from presence where id_employee=$id_employee and (work_date>=$debut and work_date<$fin)";
        $presence=$this->db->query($query);
        return $presence;
}

My controller (contrat_controller) :

public function presence($id_employee,$debut,$fin){
       $presence=$this->contrat_model->presence($id_employee,$debut,$fin);
       $i=0;
       $all=0;
       foreach ($presence as $row){
            $statut=$row;
            $all++;
            if($statut=="green"){
                $i++;
            }
        }

        $data=array("i"=>$i, "all"=>$all);
        echo json_encode($data); 
}

My view (contrat) :

function afficher(id_personnel,debut,fin)
{
        $.ajax({
        url : "<?php echo site_url('Contrat_controller/presence')?>/"+id_personnel+"/"+debut+"/"+fin,       
        type: "GET",
        dataType: "JSON",
        success: function(data)
        {
            alert(data.i);
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Erreur d\'affichage du graphe'); 
        }
    });
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Dev1996
  • 81
  • 1
  • 2
  • 12
  • Why are you writting a function inside the view? Why aren't you using the template view system? I think you're doing a lot of things wrong here. If your template is like that, when you check the html of your page, is the $.ajax function present? – Federico J. May 27 '17 at 19:06
  • Your code is vulnerable to SQL injection attacks. – tereško May 29 '17 at 12:51

1 Answers1

0

You can refer the javascript code from the answer mentioned in the link below:

Send JSON data via POST (ajax) and receive json response from Controller (MVC)

Hope this helps u.

Unnati
  • 348
  • 5
  • 18