Sunday, October 1, 2017

AngularJS – POST JSON Array To MySQL Database – PHP

Okay so I am working on a Codeigniter 3.0 application and attempting to post an array of JSON data to the database. But I am not getting an logs regarding the issue and cant seem to find out what I am doing wrong. Any help would be greatly appreciated.

Right now here is the following
Angular Array:

$scope.packageDetails = [
    {ID: 1898, Number: 909, Name: "test", category: "cat"},
    {ID: 1899, Number: 919, Name: "test 2", category: "cat 2"},
];

Angular POST Request:

$scope.SendData = function () {
    // use $.param jQuery function to serialize data from JSON 
    var purchaseOrderData = $scope.packageDetails;
    console.log(purchaseOrderData);

          $http({
            method : 'POST',
            url : '/inventory/createPurchaseOrder',
            headers: {'Content-Type':'application/x-www-form-urlencoded'},
            data : purchaseOrderData
          }).then(function(success) {
            console.log("yes");

          }, function (error) {
            console.log("no");

          });        

  };

CodeIgniter Controller:

public function createPurchaseOrder() {
        $data = new stdClass;
        if (in_array($this->session->user('permission'), array('admin'))) {
            $this->load->model('mainmodel');
            if ($this->mainmodel->create(WEBSITE_ID)) {
            $results = $this->mainmodel2->create(WEBSITE_ID);
            $data->success = true;
            };
            $data->success = true;
        } else {
            $data->error = true;
        }
        header('Content-Type: application/json');
        echo json_encode($data);
    }

CodeIgniter Model:

class Mainmodel extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function insert($id)
    {
        $this->order_website_id = $id;  
        return $this->db->insert('orders', $this);
    }

    function create($id)
    {

        $data = json_decode(file_get_contents("php://input"));


        foreach ($data as $package) {
            for ($x = 0; $x < 1; $x++) {
            $this->order_id                         = $package['ID'];
            $this->order_name                       = $package['Name'];
            $this->order_number                     = $package['Number'];
            $this->order_category                   = $package['category'];

            $data = array(
               'order_id'                   => $this->order_id,
               'order_name'                 => $this->order_name,
               'order_number'               => $this->order_number,
               'order_category'             => $this->order_category
            );      

            return $this->db->insert('orders', $data);
        }



        }

    }
}
?>

Source: AngularJS



from Angular Questions https://angularquestions.com/2017/10/01/angularjs-post-json-array-to-mysql-database-php/
via @lzomedia #developer #freelance #web #lzomedia.com

No comments:

Post a Comment