开发者

Javascript: send array through post

开发者 https://www.devze.com 2023-04-11 12:50 出处:网络
I am making a site that links an unlimited number of physical addresses to one account.When an address is added, the script checks the fields and puts the contents of each field into the corresponding

I am making a site that links an unlimited number of physical addresses to one account. When an address is added, the script checks the fields and puts the contents of each field into the corresponding array. It also counts the number of times this happens to make it easier to loop through my arrays on the server and make an insert statement for the database. Is there a way to send these arrays back to the server through post, and if so, how do I make them readable to PHP?

EDIT: Rafe Kettler's comment below helps a lot. It's exactly what I need, and makes my job a ton easier. Thanks for the help. The "Answer" I selected reflects the type of coding I will need to do and has shown me that 1) it's possible and 2) it's not all that di开发者_JAVA技巧fficult.


jQuery.post http://api.jquery.com/jQuery.post/

Sample: js:

function post_2_server(){
    $.post("/test.php", { temp: ["test1","test2"], temp2: "test" },
       function(json) {
         $.each(json.items, function(key, value) { 
           alert(key + ': ' + value); 
         });
    });
}

php:

<?php
   if(isset($_POST)){
       echo json_encode(array('items'=>array(1,2,3,4)))
   }


You could try serializing and unserializing the array

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号