douchun9719 2012-09-18 11:33
浏览 24
已采纳

PHP和Javascript:在PHP中使用Javascript字典

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条回答 默认 最新

  • duangouyan3328 2012-09-18 11:52
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端