繁体   English   中英

麻烦从javascript传递关联数组到php

[英]trouble passing associative array from javascript to php

我正在使用CodeIgniter开发应用程序。 我已经在javascript创建了一个associative array ,并将值通过ajax传递给php脚本。 在javascript中,仅当两个文本字段不为空时,才将值插入到数组中。 当我将空数组传递给php脚本并在Model中回显变量时,我得到的输出为

array([0]=> ) 

如果我通过插入值传递数组,则模型的响应为

array(['key1']=>['value1'])  

如何避免[0] 我的剧本是

if(document.getElementById("insertname").value != null &&    document.getElementById("insertnumber").value != null)
{var partner_name = new Object();
 partner_name[document.getElementById("insertname").value] = document.getElementById("insertnumber").value;
}

$.ajax({
          type:"POST",
          url:"",
          data:{Partner_name:partner_name,cus_id:id,cus_message:customermessage},
          success:function(responsee){
            alert("Message Sent and Stored");
            alert(responsee);
          }
        });

var dump value

array(1) {
  [0]=>
  string(0) ""
}

为什么不做:

<script type="text/javascript">

<?php if(count($array) > 0){ ?>
var _array = "<?php $array; ?>"; //put here what you need this is just an example
<?php }else{ ?>
var _array = "";
<?php } ?>

</script>

尝试

var data = {
    'one' : {
       'child' : '',
    },
    'two' : '',
}

$.ajax({ data : data });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM