2016-05-18 19 views
0

我想通過使用帶有可編輯數據的模態來編輯數據。使用ajax在php中使用可編輯數據的Bootstrap模態

$HTML='<script> var js_array = '.JSON_encode($result).'; 
     </script>'; 
     echo $HTML; 

,並在javascript

var id=supplier_id; 
        $.ajax({ 
      url:"index.php/Supplier/edit", 
      type:"POST", 
      data:{ID:id}, 
      dataType: 'json', 
      cache: false, 
      success: function(result) { 
      .............??????????????.................. 
      alert(js_array['SupplierCode']); 

      }, 

      }); 

}

現在我有JSON對象,但我想單獨訪問的對象,但它不工作。

我有以下格式我的數據:

var js_array = {"SupplierCode":"52","SupplierName":"GANE","Address":"79\/9 UR ST","City":"TANJORE","State":"TN","Country":"IN","PinCode":"624531","ContactPerson":"GANI","MobileNumber":"8807892105","TelephoneNumber":null,"EmailID":"[email protected]","FaxNumber":null,"Website":"www.gani.in"}; 
+0

$ HTML =' - 文本必須被引用,因此等於雙引號單引號.PHP。單引號雙引號分號 – gibberish

+0

@gibberish您能否將該評論置入回答問號 – jkdev

+0

@gibberish仍然無法獲得警報以獲取對象。 ajax代碼中是否有錯誤?你能證實嗎? – mul1103

回答

0

不知道這是否會解決你的一切,但PHP必須:

$HTML = '<script> var js_array = "' .JSON_encode($result). '";</script> 

JSON_encode創建文本,文本必須被引用,所以:等於雙引號單引號.PHP。單引號雙引號分號


而且試試這個:

$.ajax({ 
    url:"a_different_php_file.php", 
    type:"POST", 
    data:{ID:supplier_id}, 
    dataType: 'json', //this only affects data coming BACK from PHP 
    cache: false, 
    success: function(obama) { 
     alert(obama); 
    } 
}); 

a_different_php_file.php

<?php 
    $recd = $_POST['ID']; 
    echo $recd; 

請注意,你的PHP AJAX處理器文件必須是次要的PHP文件。您不能在包含JavaScript AJAX例程的相同PHP文件的不同部分中處理AJAX。

資源:

See the this post link in this related answer

+0

雅我試過你的代碼,但我仍然無法從ajax得到預期的警報。所以ajax可能是錯誤的。 – mul1103

+0

@ mul1103查看更新(已進一步更新 - F5刷新請求 – gibberish

+0

首先我有一個表格,我選擇一行,然後點擊編輯按鈕,出現可編輯的數據模式,爲此我使用JSON並且已經是i – mul1103

0

也就是說解析JSON data.Try本的好方法: 在PHP中:

JSON_encode($result); 

在javascript中:

$.ajax({ 
     url:"index.php/Supplier/edit", 
     type:"POST", 
     data:{ID:id}, 
     dataType: 'json', 
     cache: false, 
     success: function(result) { 
     var parsed_json=JSON.parse(result) 
     var supplierCode=parsed_json.SupplierCode; 
     alert(supplierCode); 
     }, 

     }); 
相關問題