M
M
Maxim Iralov2021-02-19 12:19:21
AJAX
Maxim Iralov, 2021-02-19 12:19:21

How to correctly pass json and get it in the controller?

Pass json array to controller (namesDp)

$http({
            method: "POST",
            url: "http://localhost/cabinet/contracts/addContract",
            data: $.param({
                ......
                namesDp: $scope.data.model
            }),


In the controller I do this Well, I pass this array to the model

$dpList = json_decode($_POST['namesDp']);


if($this->model->addContract($dpList, $contractNumber,$contractTitle, $contractDate . . . ..


Here is the model

public function addContract( $dpList, $contractNumber,$contractTitle, $contractDate . . . . .. . . ) {


    foreach($dpList as $value) {

      $sql = "INSERT INTO contract(department, numberContract, nameContract, dateConclusion)
    VALUES(:department, :numberContract, :nameContract, :dateConclusion)
    ";
      $stmt = $this->db->prepare($sql);
      $stmt->bindValue(":department", $value, PDO::PARAM_INT);
      $stmt->bindValue(":numberContract", $contractNumber, PDO::PARAM_INT);
      $stmt->bindValue(":nameContract", $contractTitle, PDO::PARAM_STR);
      $stmt->bindValue(":dateConclusion", $contractDate, PDO::PARAM_STR);
      $stmt->execute();

. . ... .


There is an error
json_decode () expects parameter 1 to be string, array given in

Perhaps there are more errors .. when iterating over an array

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question