0

We are sending data through Ajax call and getting response as array. we want to loop that array and load into array input field. but data not loading into input.

 <input type="hidden" name="loadchild[]" id="loadchild[]" >

this textbox is in loop

$.ajax({
    type: "POST",
    url: "asign.php",
    data: {plan_id: plan_id},
    dataType: "json",
    success: function (dta)
    {
        /*
         here  dta['insert_id'] is single value and 
         dta['child_ids'] are multiple values and i am getting as [1,2,3,4,5]     */

        for (var i = 0; i < dta['child_ids'].length; i++)
        {
            $("#loadchild[" + i + "]").val(dta['child_ids'][i]);
        }
    }
});

here i am getting as [object Object] when asigning value to textbox

Please let me know how to pass array value one by one into

7
  • 1
    Through PHP you can't send array either you have to use json_encode or serialize that array then send to AJAX and use jQuery Commented Oct 24, 2016 at 11:53
  • $("#loadchild["+i+"]") This is not a valid jQuery selector, use .eq(i) instead Commented Oct 24, 2016 at 11:53
  • Plus, you can set id to some like this: loadchild[] Commented Oct 24, 2016 at 11:55
  • @Sunil Pachlangia OP is not sending the array to the server, he is sending another id, then wanting to return an array and output them in a bunch of input Commented Oct 24, 2016 at 11:55
  • $("#loadchild[]").eq(i).val(dta['child_ids'][i]); is this valid..? Commented Oct 24, 2016 at 11:56

1 Answer 1

2

try this

1)you have to escape the brackets like this

for(var i=0;i<dta['child_ids'].length;i++)
  {
   //$("#loadchild["+i+"]").val(dta['child_ids'][i]);

   $("#loadchild\\[\\]").eq(i).val(dta['child_ids'][i]);

  }
Sign up to request clarification or add additional context in comments.

1 Comment

glad to help you @venkyrao

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.