0

What is wrong with my code that i can not show anything in resultTXT

txtfld shows the array

[{
    "user_id": "2790",
    "freelancer_name": "",
    "order_id": "9121",
    "orderamount": "0.00",
    "payment_method": " ....... "
}]

I want the user ID to be in resultTXT

ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        $.post('userfind.php', function(data) {
            $("#txtfld").val(data);

            var json = data,
            obj = JSON.parse(json);
            alert(""+obj.user_id);
            $("#resultTXT").val(obj.user_id);
        },'json');
    }
};
ajaxRequest.open("POST", "userfind.php", true);
ajaxRequest.send(null); 

Please help me what should be changed.

1
  • 1
    Why do you have jQuery inside of a ajaxRequest.open? Use either jQuery AJAX or vanilla JS AJAX, not both. Commented Jul 17, 2012 at 14:26

2 Answers 2

3

I see a few things wrong with this. First, why do you POST to userfind.php twice? Why are you using vanilla JS AJAX and jQuery AJAX? Just use one.

Second, the ,'json' in $.post means jQuery will parse the JSON for you, you don't need JSON.parse.

Third, your JSON is an array (of objects), so you need to get the array element first, then the user_id property.

$.post('userfind.php', function(data) {
    $("#txtfld").val(data);  // data is an object,
                             // so this will just put [object Object] in the field,
                             // probably not what you want

    alert(data[0].user_id);  // data is an array of (one) object(s)
    $("#resultTXT").val(data[0].user_id);
},'json');
Sign up to request clarification or add additional context in comments.

1 Comment

$("#txtfld").val(data); shows the array i can see the array elements in the box. and alert(data[0].user_id); // data is an array of (one) object(s) $("#resultTXT").val(data[0].user_id); },'json'); doesn't show anything
0

I think that is because the json is an array so maybe try obj[0].user_id

Comments

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.