0

I want to edit data by using modal with editable data.

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

and in 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']);

            },  

            });

}

Now I have JSON object but i tried to access the objects individually but it is not working.

I have my data in the following format:

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"};
mul1103
  • 11
  • 6
  • $HTML=' -- the text must be quoted, so equals double-quote single-quote .PHP. single-quote double-quote semicolon – cssyphus May 18 '16 at 05:09
  • @gibberish Could you please make that comment into an answer question mark – jkdev May 18 '16 at 05:15
  • @gibberish Still i could not get alert to get the objects . Is there any mistakes in ajax code? could you confirm it? – mul1103 May 18 '16 at 05:17

2 Answers2

0

Not sure if this will solve everything for you, but the PHP must be:

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

JSON_encode creates text, and text must be quoted, so: equals double-quote single-quote .PHP. single-quote double-quote semicolon


And try this:

$.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;

Note that your PHP ajax processor file must be a secondary PHP file. You cannot process AJAX in a different section of the same PHP file that contains the javascript AJAX routine.

Resource:

See the this post link in this related answer

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Ya I tried your code, but still i couldn't get expected alert from ajax. So the ajax may be error. – mul1103 May 18 '16 at 05:21
  • @mul1103 See update (which has been further updated - F5 refresh pls. – cssyphus May 18 '16 at 05:25
  • First of all i have a table . I select a row , then click on edit button appears the editable data modal. For that i am using JSON and already i posted the data now i want to get the selected rows data. – mul1103 May 18 '16 at 05:33
  • `alert(obama.SupplierCode);` -- that assumes that the data sent back from the PHP side is the JSON string displayed at the bottom of your question -- `alert('Name is: ' + obama.SupplierName);` – cssyphus May 18 '16 at 05:38
  • Hi i tried the above. It is not working. When i refer JSON tutorial the json objects within curly braces is single quoted not double quoted. – mul1103 May 18 '16 at 05:49
  • When i hide the dataType: json, it alerts the name is undefined. What is the problem with this code? – mul1103 May 18 '16 at 06:03
0

That is a bad way of parsing JSON data.Try this: In php:

JSON_encode($result);

In 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);
        },  

        });
Javasamurai
  • 666
  • 7
  • 21