Home » Php » php – Nested JSON data via AJAX with jquery but getting undefined value

php – Nested JSON data via AJAX with jquery but getting undefined value

Posted by: admin February 25, 2020 Leave a comment

Questions:

Javascript

JSON

Web Inspect

I have got an error message.
Uncaught ReferenceError: products is not defined.

$(document).ready(function(){
    $("div").on("click", "a", function(){
        var delivery_id = $(this).attr("id");
        $.ajax({
            url:"http://localhost:8888/dashboard/fetch_edit.php",
            method:"POST",
            data:{products:products},}
        });
    });

JSON


{ 
     "id": "2",
     "send_id": "10",
     "tracking_id": "TI-000000010",
     "user_id": "10",
     "username": "istiaqahmed",
     "email": "[email protected]",
     "phone": "0176430886",
     "company_name": "EVALY.COM.BD",
     "company_phone": "01747588386",
     "company_address": "Dhanmondi 28",
     "com_email": "[email protected]",
     "delivery_type": "Standard",
     "packing": "regular",
     "product_weight": "1",
     "preferred_time": "morning",
     "delivery_charge": "60",
     "customer_name": "Zenith Jhony",
     "to_address1": "75\/1 Jafrabad Pulpar Pabna House Goli",
     "to_phone": "01776065208",
     "to_zone": "Dhaka",
     "to_post_code": "1207",
     "date": "2020-01-09 23:26:10",
     "u_status": "Approve",
     "notes": "note",
     "products": [ 
          { 
               "product_id": "1",
               "product_name": "shampoo",
               "product_quantity": "2",
               "product_price": "2333",
               "customer_send_id": "10" 
          },
          {
                "product_id": "2",
               "product_name": "assf",
               "product_quantity": "1",
               "product_price": "232",
               "customer_send_id": "10" 
          } 
     ] 
 }
How to&Answers:

You are trying to reference an attribute which does not exist.

lets say you assign that JSON object o a variable like this:

obj = {
  .... the JSON
}

you could then pass the JSON as follows

data:{products:obj.products}

Answer:

Hope this help you.

var products_obj = '{ "id": "2", "send_id": "10", "tracking_id": "TI-000000010", "user_id": "10", "username": "istiaqahmed", "email": "[email protected]", "phone": "0176430886", "company_name": "EVALY.COM.BD", "company_phone": "01747588386", "company_address": "Dhanmondi 28", "com_email": "[email protected]", "delivery_type": "Standard", "packing": "regular", "product_weight": "1", "preferred_time": "morning", "delivery_charge": "60", "customer_name": "Zenith Jhony", "to_address1": "75\/1 Jafrabad Pulpar Pabna House Goli", "to_phone": "01776065208", "to_zone": "Dhaka", "to_post_code": "1207", "date": "2020-01-09 23:26:10", "u_status": "Approve", "notes": "note", "products": [ { "product_id": "1", "product_name": "shampoo", "product_quantity": "2", "product_price": "2333", "customer_send_id": "10" }, { "product_id": "2", "product_name": "assf", "product_quantity": "1", "product_price": "232", "customer_send_id": "10" } ] }';
products_obj = JSON.parse(products_obj);
var products= products_obj.products;
console.log(products);

$(document).ready(function(){
    $("div").on("click", "a", function(){
        var delivery_id = $(this).attr("id");
        $.ajax({
            url:"http://localhost:8888/dashboard/fetch_edit.php",
            method:"POST",
            data:{products:products},}
        });
});