简体   繁体   中英

trouble passing associative array from javascript to php

I am developing an application using CodeIgniter . I have created an associative array in javascript and passing the values to php script via ajax . In javascript, the values are inserted into the array only if two text fields are not empty. When I pass an empty array to php script and echo the variable in Model, I am getting the output as

array([0]=> ) 

and if i pass the array by inserting values the response from model is

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

how can i avoid [0] ? My script is

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) ""
}

why not doing :

<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>

try

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

$.ajax({ data : data });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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