使用ajax在php中使用可编辑数据的Bootstrap Modal


Bootstrap Modal with editable data in php using ajax

我想通过使用具有可编辑数据的模态来编辑数据。

$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":"gani@fun.in","FaxNumber":null,"Website":"www.gani.in"};

不确定这是否能为您解决所有问题,但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。

资源:

请参阅相关答案中的this post链接

这是解析JSON数据的一种糟糕方法。试试这个:在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);
        },  
        });