0

I am storing some key and values in javascript dictionary

var list_of_addressbook_entries = {};

and i am passing that dictionary on a php page

But when i am trying to get the key and value of javascript dict like

 $attendee_list = $_POST['dict_of_externals'];
   $shared_attendee_list = $_POST['list_of_shared_addressbook_entries'];
   $org_attendee_list = $_POST['dict_of_org_users'];

   echo json_encode($attendee_list);


   return false;

As a response i am getting only "[object Object]"

i just want to know the way how can i extract the values in php ?

in my javascript

for(key in list_of_addressbook_entries)
        {
          alert("key " + key
             + " has value "
          + list_of_addressbook_entries[key]);
        }

is printing the key and values

Updated

var list_of_addressbook_entries_privilege = [];
function showDetails(guest_name){
        var user_name = "<?php echo $_SESSION["user_name"]; ?>" ;
        var name = prompt("Please use M for moderator and A for attendee", "Type you name here");
        if (!name == null && !name== '' || name == 'M' || name == 'A'){

        var ahtml='<div  id ="div_id_'+guest_name+'" onclick =remove_addressbook_user("'+guest_name+'") class="addr_list" style="display:block; background: none repeat scroll 0% 0% peachpuff;margin-bottom:1px;">'
        ahtml = ahtml + '<span>'+guest_name+'</span>'
        ahtml = ahtml + '<input type = "hidden" id ="user_id_'+guest_name+'"   value = "'+guest_name+'"  /></div>'
        $(".address_book_list").append(ahtml);
        $("#div_id_"+guest_name).hide();
        list_of_addressbook_entries[guest_name] = name ;
        }
        else{
            alert("You have entered the worong value ");
            return false;
        }
        $('#dict_of_externals').val(list_of_addressbook_entries);

        for(key in list_of_addressbook_entries)
        {
          alert("key " + key
             + " has value "
          + list_of_addressbook_entries[key]);
        }


}
3
  • 2
    list_of_addressbook_entries is an object, not sure how you want to store it in a field value. JSON.stringify(list_of_addressbook_entries) will give a string representation of that object. Commented Sep 18, 2012 at 11:45
  • @FabrícioMatté i have a hidden field in my form and from javascript i am doing $('#dict_of_externals').val(list_of_addressbook_entries); to add values to the hidden field Commented Sep 18, 2012 at 11:46
  • @User: What fabrico said still stands... You need to stringify the js object if youre going to store it in a field value. Commented Sep 18, 2012 at 11:49

3 Answers 3

1

As you can iterate the pair of key values with for in, list_of_addressbook_entries is an object.

You can't put an object inside a value property. The value property supports strings only.

Hence JS converts your object to a string, for which the string representation is [object Object].

If you want a string representation of the JSON object you will use JSON.stringify:

$('#dict_of_externals').val(JSON.stringify(list_of_addressbook_entries));

if you want to access the JSON object's properties later, you'll have to parse the string again with JSON.parse (or jQuery.parseJSON), hence I'd recommend keeping the object in the memory in case you need to access its property values later.

Here's a small JS library if you need to JSON.stringify support for IE<=7:
https://github.com/douglascrockford/JSON-js

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

Comments

1

Javascript is on the client and php executes on the server, if you want to pass data from javascript to your server you'll need to make an AJAX call to the server or pass it through a form.

How are you calling the php code, how are you passing the data to the server?

2 Comments

i have a hidden field in my form and from javascript i am doing $('#dict_of_externals').val(list_of_addressbook_entries); to add values to the hidden field
You'll need to stringify your json object in order to send it to the server, if you store the object directly on the hidden element the string that's going to be stored is [Object object]. Keep in mind that hidden elements and text elements only are able to store text, and json objects aren't just text until they are stringified
0

The answer is pretty simple:

The php function json_encode is encoding the object, and thus you are echoing an json object, that's why php is echoing [object Object].

If you want the output in the json notation output you have to do something like

//in case you passed the $attendee_list without encoding
$json_obj = json_encode($attendee_list);

echo json_decode($json_obj);

//otherwise
echo json_decode($attendee_list);

I hope it helped, cheers

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.