0

I will send value type ajax but when data return come back will return json How to append value to input text.

$.ajax({
    type: "POST",
    url: "find_data.php",
    data: "location=" + location_code,
    success : function(data){
        $.each(data, function(key, values){
                $('#card').val(values.card);
         }); 
      },
    dataType: "json",  
    error:function(error){
            alert(error);
    }
});

Sorry for my english. This is will show like this => enter image description here

9
  • 1
    share some HTML please Commented Dec 8, 2015 at 7:35
  • show us how your are printing the data in ajax call. Commented Dec 8, 2015 at 7:35
  • 1
    Also be careful about multiple id's in same webpage Commented Dec 8, 2015 at 7:35
  • include console.log(data) Commented Dec 8, 2015 at 7:37
  • Use console.log(values.card); after $('#card').val(values.card); line and check into console. Commented Dec 8, 2015 at 7:39

1 Answer 1

1

Seems you have an invalid markup as in having same IDs for multiple input[type=text] elements. The valid thing is IDs should be unique for each element or use a common class name.

If this is the case then i would suggest you to change the ID to class instead:

<input type='text' class='card'>

now in your ajax success you can change to this:

  success : function(data){
    $.each(data, function(key, values){
         $('.card').get(key).val(values.card);
     }); 
  },

this line can be changed to this too:

$('.card')[key].value = values.card;

You have to post your markup for this, this case is my assumption.


What could be the issue?

If you assign same ID to multiple elements and when you make a selector of that ID, That always returns a single element in the page elements lookup. When lookup gets started and it finds the ID, it stops the lookup as per valid markup every element should have unique IDs.

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

2 Comments

Yes I have multiple elements What cand i do ?
I have multiple elements when return value And i wanna show multiple text box too.

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.