2017-05-30 177 views
0

保存到表單和數據網格時出現錯誤 我不知道,錯誤是什麼? 我使用ExtJS的4和Zend框架2上傳文件(附件)錯誤

這是消息錯誤:

sourceClass: "Ext.JSON", 
sourceMethod: "decode", 
msg: "You're trying to decode an invalid JSON String: Wa…session":true, 
"other":null,"processingTime":null}", 
message: "You're trying to decode an invalid JSON String: Wa…session":true, 
"other":null,"processingTime":null}"} 
message: "You're trying to decode an invalid JSON String: 
Warning: Invalid argument supplied for foreach() in Purchase.php on line 331↵ 
{"success":false,"data":{"no_po":null,"date_po":null,"supplier":null,"shipping_to":null,"total_price_head":null,"vat":null,"net_price":null,"id_po":null,"file_ext":"png","file_type":"image\/png","file_size":633237,"file_name":"2015-02-23-Negative-World-left-hand.png"},"messages":[],"pageSize":null,"start":null,"total":null,"session":true,"other":null,"processingTime":null}" 
msg: "You're trying to decode an invalid JSON String: 
Warning: Invalid argument supplied for foreach() in Purchase.php on line 331↵ 
{"success":false,"data":{"no_po":null,"date_po":null,"supplier":null,"shipping_to":null,"total_price_head":null,"vat":null,"net_price":null,"id_po":null,"file_ext":"png","file_type":"image\/png","file_size":633237,"file_name":"2015-02-23-Negative-World-left-hand.png"},"messages":[],"pageSize":null,"start":null,"total":null,"session":true,"other":null,"processingTime":null}" 
sourceClass: "Ext.JSON" 
sourceMethod: "decode" 

和此我的代碼:

Purchase.php

public function savePurchaseOrder($header,$detail) 
{ 
    header('Content-Type: text/html'); 
    try{ 

     if(!$this->_user_id){ 
      $this->result->messages[] = 'Session Expired, Please re-login'; 
      return; 
     } 

     $_header = Zend_Json::decode($header, Zend_Json::TYPE_ARRAY); 
     $_detail = Zend_Json::decode($detail, Zend_Json::TYPE_ARRAY); 

     $userLogin = $this->_user_id; 

     $table_header = new Zend_Db_Table('vending_tr_purchase_product_order_id'); 
     $table_detail = new Zend_Db_Table('vending_tr_purchase_product_order'); 
     $table_detail_pr = new Zend_Db_Table('vending_tr_purchase_product'); 
     //print_r($_detail); 

    if (isset($_FILES['file_path'])) 
    { 
      $errors = array(); 
      // define file attribute 
      $file_name = $_FILES['file_path']['name']; 
      $file_size = $_FILES['file_path']['size']; 
      $file_tmp = $_FILES['file_path']['tmp_name']; 
      $file_type = $_FILES['file_path']['type']; 
      $file_ext = strtolower(end(explode('.', $_FILES['file_path']['name']))); 

     $_newdata = array(
      'no_po' => $_header['no_po'], 
      'date_po' => $_header['date_po'], 
      'supplier' => $_header['supplier'], 
      'shipping_to' => $_header['shipping_to'], 
      'total_price_head' => $_header['total_price_head'], 
      'vat' => $_header['vat'], 
      'net_price' => $_header['net_price'], 
      'id_po' => $_header['id_po'], 
      //'file_path' => $_header['file_path'], 
      'file_ext' => $file_ext, 
      'file_type' => $file_type, 
      'file_size' => $file_size, 
      'file_name' => $file_name 

     ); 

     $ext = array(
       "bmp", 
       "jpeg", 
       "jpg", 
       "png", 
       "gif", 
       "xls", 
       "xlsx", 
       "doc", 
       "docx", 
       "pdf", 
       "ppt", 
       "pptx", 
       "zip", 
       "rar", 
       "txt", 
       "rtf" 
     ); 
      // check the extension type on upload process 
      if (in_array($file_ext, $ext) === false) 
      { 
       $errors[] = 'File format "' . $file_ext . '" is not allowed'; 
       return $this->result->messages[] = $errors; 
      } 
      // check the file size is not more than 25 MB = 26214400 bytes, 1 MB = 1048576 bytes 
      if ($file_size > 26214400) 
      { 
       $errors[] = 'Sorry, the allowed max size is 25 MB'; 
       return $this->result->messages[] = $errors; 
      } 

     if($_header['action'] == 'C'){ 
      $record = array_merge($_newdata, array(
       'row_created_by' => $userLogin, 
       'row_created_datetime' => date("YmdHis", strtotime($this->_datetime)) 
      )); 
      $table_header->insert($record); 
      $header_id = $this->_db->lastInsertId(); 
      $this->result->success = true; 
     } 

     if($_header['action'] == 'U'){ 
      $where = array("id_po = " . $this->_db->quote($_header['id_po'])); 
      $record = array_merge($_newdata, array(
       'row_changed_by' => $userLogin, 
       'row_changed_datetime' => date("YmdHis", strtotime($this->_datetime)) 
      )); 
      $table_header->update($record,$where); 
      $this->result->success = true; 
     } 

     foreach ($_detail as $key) { 

      $this->_db->beginTransaction(); 
      //$key['data']['warehouse'] = $_header['warehouse']; 

      $_insertDetail = array(
       'qty_order' => $key['data']['qty_order'], 
       'no_pr' => $key['data']['no_pr'], 
       'purchase_product_name' => $key['data']['purchase_product_name'], 
       'package' => $key['data']['package'], 
       'unit' => $key['data']['unit'], 
       'qty_approve' => $key['data']['qty_approve'], 
       'purchase_id_tr' => $key['data']['purchase_id_tr'], 
       'qty_hasil' => $key['data']['qty_hasil'], 
       'unit_price' => $key['data']['unit_price'], 
       'total_price' => $key['data']['total_price'], 
       'warehouse_name' => $key['data']['warehouse_name'] 
      ); 

      $_updateDetailPO = array(
       'qty_order' => $key['data']['qty_order'], 
       'qty_hasil' => $key['data']['qty_hasil'] 
      ); 

      if($key['type'] == 'U'){ 
       $where = array("purchase_id_tr = " . $this->_db->quote($key['data']['purchase_id_tr'])); 
       $table_detail_pr->update($_updateDetailPO,$where); 
       $this->result->success = true; 
      } 

      if(!empty($key['data']['id'])){ 
       $where = array("id = " . $this->_db->quote($key['data']['id'])); 
       $table_detail->update($_insertDetail,$where); 
       $this->result->success = true; 
      }else{ 
       $_insertDetail['id_po'] = $header_id; 
       $table_detail->insert($_insertDetail); 
       $this->_db->commit(); 
       $updateStock = $this->updatePR($key['data']); 
       $this->result->success = true; 
      } 

      if($key['type'] == 'D'){ 
       $where = array("id = " . $this->_db->quote($key['data']['id'])); 
       $table_detail->delete($where); 
       $this->_db->commit(); 
       $this->result->success = true; 
      } 


     } 
     return $_newdata; 

    } 


    } 
    catch(Exception $_e) { 
     $this->result->messages[] = new Sasmita_Transaction_Message('E', $_e->getMessage(), __CLASS__ . '/' . __FUNCTION__, __LINE__); 
    } 
} 

public function updatePR($data) 
{ 
    //var_dump($data); 
    //exit; 
    $_sStock = $this->_db->select('a vending_tr_purchase_product','*') 
    ->where('a.purchase_id_tr = ?',$data['purchase_id_tr']) 
    //->where('a.warehouse_id = ?',$data['warehouse']) 
    ->fetch(); 

    $table = new Zend_Db_Table('vending_tr_purchase_product'); 

    if(is_array($_sStock)){ 
     $pengurangan = $data['qty_hasil']-$data['qty_order']; 
     $_updateDetailPO_R = array(
       'qty_order' => $data['qty_order'], 
       'qty_hasil' => $pengurangan 
      ); 
     $where = array("purchase_id_tr = " . $this->_db->quote($data['purchase_id_tr'])); 
     $a = $table->update($_updateDetailPO_R,$where); 
     $this->result->success = true; 
     //print_r ($a); 
     //print_r($a->__toString()); 

    }else{ 
     $this->_db->beginTransaction(); 
     $_insertArray = array(
       'purchase_product_id' => $data['product_id'], 
       'brand' => $data['data']['brand'], 
       'package' => $data['data']['package'], 
       'purchase_product_name' => $data['data']['product_name'], 
       'qty_request' => $data['qty_request'], 
       'qty_approve' => $data['qty_approve'], 
       'qty_order' => $data['qty_approve'], 
       'qty_hasil' => $data['qty_approve'], 
       'approvel' => $data['approvel'], 
       'unit_price' => $data['unit_price'], 
       'unit' => $data['unit'] 
      ); 
     $table->insert($_insertArray); 
     $this->_db->commit(); 
     $this->result->success = true; 
    } 

    return $this->result->success; 
} 

InputPO .js

requires: [ 
    'Ext.form.Panel', 
    'Ext.form.FieldContainer', 
    'Ext.form.field.Date', 
    'Ext.form.field.File', 
    'Ext.form.field.Hidden', 
    'Ext.grid.Panel', 
    'Ext.toolbar.Toolbar', 
    'Ext.button.Button', 
    'Ext.toolbar.Separator', 
    'Ext.selection.CheckboxModel', 
    'Ext.form.field.Number', 
    'Ext.grid.column.Number', 
    'Ext.grid.column.Date', 
    'Ext.grid.column.Boolean', 
    'Ext.grid.View', 
    'Ext.grid.plugin.RowEditing', 
    'Ext.grid.feature.Summary' 
], 

fileUpload: true, 
height: 473, 
width: 633, 
title: '', 

initComponent: function() { 
    var me = this; 

    Ext.applyIf(me, { 
     items: [ 
      { 
       xtype: 'form', 
       bodyPadding: 10, 
       title: '', 
       items: [ 
        { 
         xtype: 'fieldcontainer', 
         fieldLabel: '', 
         layout: { 
          type: 'hbox', 
          align: 'stretch' 
         }, 
         items: [ 
          { 
           xtype: 'container', 
           items: [ 
            { 
             xtype: 'textfield', 
             width: 250, 
             fieldLabel: 'No. PO', 
             labelWidth: 70, 
             name: 'no_po' 
            }, 
            { 
             xtype: 'textfield', 
             hidden: true, 
             width: 250, 
             fieldLabel: 'ID. PO', 
             labelWidth: 70, 
             name: 'id_po' 
            }, 
            { 
             xtype: 'datefield', 
             width: 250, 
             fieldLabel: 'Date', 
             labelWidth: 70, 
             name: 'date_po' 
            }, 
            { 
             xtype: 'textfield', 
             width: 250, 
             fieldLabel: 'Supplier', 
             labelWidth: 70, 
             name: 'supplier' 
            } 
           ] 
          }, 
          { 
           xtype: 'container', 
           margin: '0 0 0 10', 
           items: [ 
            { 
             xtype: 'textfield', 
             width: 250, 
             fieldLabel: 'Total Price', 
             labelWidth: 70, 
             name: 'total_price_head' 
            }, 
            { 
             xtype: 'textfield', 
             width: 250, 
             fieldLabel: 'VAT', 
             labelWidth: 70, 
             name: 'vat' 
            }, 
            { 
             xtype: 'textfield', 
             width: 250, 
             fieldLabel: 'Net Price', 
             labelWidth: 70, 
             name: 'net_price' 
            } 
           ] 
          } 
         ] 
        }, 
        { 
         xtype: 'container', 
         items: [ 
          { 
           xtype: 'textfield', 
           hidden: true, 
           width: 511, 
           fieldLabel: 'Shipping To', 
           labelWidth: 70, 
           name: 'shipping_to' 
          }, 
          { 
           xtype: 'filefield', 
           width: 511, 
           fieldLabel: 'Attachment', 
           labelWidth: 70, 
           name: 'file_path' 
          } 
         ] 
        }, 
        { 
         xtype: 'hiddenfield', 
         name: 'action' 
        } 
       ] 
      }, 
      { 
       xtype: 'gridpanel', 
       height: 250, 
       itemId: 'detailProduct', 
       autoScroll: true, 
       bodyBorder: false, 
       title: 'Detail Product', 
       store: 'vending.purchase.DetailProduct', 
       dockedItems: [ 
        { 
         xtype: 'toolbar', 
         dock: 'top', 
         items: [ 
          { 
           xtype: 'button', 
           itemId: 'addDM', 
           iconCls: 'icon-add', 
           text: 'Add' 
          }, 
          { 
           xtype: 'tbseparator' 
          }, 
          { 
           xtype: 'button', 
           itemId: 'btnDelete_detailp', 
           iconCls: 'icon-delete', 
           text: 'Delete' 
          } 
         ] 
        } 
       ], 
       selModel: Ext.create('Ext.selection.CheckboxModel', { 

       }), 
       columns: [ 
        { 
         xtype: 'gridcolumn', 
         dataIndex: 'purchase_id_tr', 
         text: 'Id' 
        }, 
        { 
         xtype: 'gridcolumn', 
         hidden: true, 
         dataIndex: 'id_po', 
         text: 'Id' 
        }, 
        { 
         xtype: 'gridcolumn', 
         dataIndex: 'warehouse_name', 
         text: 'Warehouse' 
        }, 
        { 
         xtype: 'gridcolumn', 
         hidden: true, 
         dataIndex: 'qty_hasil', 
         text: 'Id' 
        }, 
        { 
         xtype: 'gridcolumn', 
         dataIndex: 'no_pr', 
         text: 'No. PR' 
        }, 
        { 
         xtype: 'gridcolumn', 
         dataIndex: 'purchase_product_name', 
         text: 'Product Name' 
        }, 
        { 
         xtype: 'gridcolumn', 
         hidden: true, 
         dataIndex: 'string', 
         text: 'Brand' 
        }, 
        { 
         xtype: 'gridcolumn', 
         hidden: true, 
         dataIndex: 'package', 
         text: 'Package' 
        }, 
        { 
         xtype: 'gridcolumn', 
         hidden: true, 
         dataIndex: 'qty_approve', 
         text: 'Quantity Remaining' 
        }, 
        { 
         xtype: 'gridcolumn', 
         dataIndex: 'unit', 
         text: 'Unit Order' 
        }, 
        { 
         xtype: 'gridcolumn', 
         hidden: true, 
         dataIndex: 'qty_hasil', 
         text: 'Quantity Remaining' 
        }, 
        { 
         xtype: 'gridcolumn', 
         dataIndex: 'qty_order', 
         text: 'Quantity', 
         editor: { 
          xtype: 'numberfield' 
         } 
        }, 
        { 
         xtype: 'gridcolumn', 
         dataIndex: 'unit_price', 
         text: 'Unit Price', 
         editor: { 
          xtype: 'numberfield', 
          name: 'unit_price' 
         } 
        }, 
        { 
         xtype: 'gridcolumn', 
         summaryType: 'sum', 
         dataIndex: 'total_price', 
         text: 'Total Price', 
         editor: { 
          xtype: 'numberfield', 
          name: 'total_price' 
         } 
        }, 
        { 
         xtype: 'numbercolumn', 
         hidden: true, 
         dataIndex: 'number', 
         text: '' 
        }, 
        { 
         xtype: 'datecolumn', 
         hidden: true, 
         dataIndex: 'date', 
         text: 'Date' 
        }, 
        { 
         xtype: 'booleancolumn', 
         hidden: true, 
         dataIndex: 'bool', 
         text: 'Boolean' 
        } 
       ], 
       plugins: [ 
        Ext.create('Ext.grid.plugin.RowEditing', { 
         clicksToEdit: 1 
        }) 
       ], 
       features: [ 
        { 
         ftype: 'summary' 
        } 
       ] 
      }, 
      { 
       xtype: 'gridpanel', 
       hidden: true, 
       title: 'Lokasi Pengiriman', 
       columns: [ 
        { 
         xtype: 'gridcolumn', 
         dataIndex: 'string', 
         text: 'Nama gudang' 
        }, 
        { 
         xtype: 'gridcolumn', 
         dataIndex: 'string', 
         text: 'Alamat' 
        }, 
        { 
         xtype: 'gridcolumn', 
         dataIndex: 'string', 
         text: 'Shipping To' 
        }, 
        { 
         xtype: 'numbercolumn', 
         hidden: true, 
         dataIndex: 'number', 
         text: 'Number' 
        }, 
        { 
         xtype: 'datecolumn', 
         hidden: true, 
         dataIndex: 'date', 
         text: 'Date' 
        }, 
        { 
         xtype: 'booleancolumn', 
         hidden: true, 
         dataIndex: 'bool', 
         text: 'Boolean' 
        } 
       ] 
      } 
     ] 
    }); 

    me.callParent(arguments); 
} 

}); 

,這是我的控制器JS行動點擊按鈕保存:

onButtonSave: function(button, e, eOpts) { 

console.log("suksesss"); 
var me = this; 
var _panel = me.getMainPanel(); 
var _tabpanel = _panel.down("#tabmaintain"); 
var _activetab = _tabpanel.getActiveTab(); 
var form = _activetab.down('form').getForm(); 
var values = form.getValues(); 
var myMask = new Ext.LoadMask(_activetab, {msg:"Please wait..."}); 


if(form.isValid()) 
{ 
    myMask.show(); 
    form.submit({ 
     url: '/jsonresult/Sasmita_Vending_Purchase/saveGoodsReceived', 
     success: function(fp, o) { 
      myMask.hide(); 
      //me.fnReloadTabAttachment(); 
      _activetab.close(); 
     }, 
     failure: function(fp,o){ 
      myMask.hide(); 
      if(!o.result.session) 
      { 
       Ext.Msg.alert('Error', o.result.messages); 
      } 
      else 
      { 
       if (o.result.messages[0].text) 
       { 
        Ext.Msg.alert('Error', o.result.messages[0].text); 
       } 
       else 
       { 
        Ext.Msg.alert('Error', o.result.messages); 
       } 
      } 
     } 
    }); 
} 
}, 

據說這裏錯誤時的foreach ..

回答

0

你試圖解碼無效的JSON字符串:

跟着:

警告:在第331行上的Purchase.php中爲foreach()提供了無效參數012'{「success」:false,「data」:{「no_po」:null,「date_po」:null,「supplier」空, 「shipping_to」:NULL, 「total_price_head」:NULL, 「缸」:NULL, 「net_price」:NULL, 「id_po」:NULL, 「file_ext」: 「PNG」, 「FILE_TYPE」: 「圖像/ PNG」, 「FILE_SIZE」:633237, 「FILE_NAME」: 「2015年2月23日 - 負 - 世界 - 左hand.png」}, 「消息」:[], 「pageSize的」:空, 「啓動」:空,「總「:null,」session「:true,」other「:null,」processingTime「:null}」

這可能意味着您的代碼在警告後期望解析JSON,這會導致組合的無效JSON字符串。

解決的辦法是檢查是否在收集你foreach上是一個集合(如數組)你觸發之前:

if(is_array($_detail)) { 
    foreach ($_detail as $key) { 
     // ... 
    } 
} else { 
    // Do something else 
} 
+0

嗨@ Dymen1, 我做了,解決的辦法是檢查集合foreach,現在沒有錯誤或輸出任何東西,當我檢查我的數據庫沒有什麼要保存到我的數據庫(不添加新記錄).. –

+0

你需要檢查你的dB提交的結果(通過函數檢查你的狀態事務或通過提交調用的返回) – Dymen1