您的位置:首页 > 编程语言 > PHP开发

[jQ/PHP]再谈使用JS数组储值的运用(提交PHP处理)

2014-11-22 16:25 387 查看
---------------------------------------------------------------------------------------------------

从一个例子中看JS数组和对象的分工合作:

/**
* JS数组与对象使用.(传递多条json数据,实例局部)
* @黑眼诗人 <www.chenwei.ws>
*/
function importL() {
if(confirm('Sure?')) {
var arr = [];
var json = {};

var type_code = $('select[name="type_code"]');   //select标签节点
var le = $('input[name="le[]"]');          //checkbox节点

$.each(le, function(i, n) {
if(n.checked) {
info = {"type_code": type_code.val(), "code": $(this).val(), "the_name": $(this).attr('the_name')};
var info = JSON.stringify(info);      //转json字符串
arr.push(info);                  //多个json字符串存入数组
}
json.all = arr;                  //将整个数组存入json对像, key为all (原因是ajax传参格式为json)
});

if(arr.length == 0) {
my_custom_tips('error1');
}else if(type_code.val() == 0) {
my_custom_tips('error2');
}else{
$.ajax({
type: 'post',
url: base_url + '?d=admin&c=api&m=ajax_import',
data: json,
success: function(data) {
console.log(data);
             }
});
}
}
}


成功接收到数据后, 处理就简单了:

/**
* PHP处理数据
* @黑眼诗人 <www.chenwei.ws>
*/
public function ajax_import()
{
  $info = $this->input->post('all');

  foreach($info as $val)
  {
  $arr[] = json_decode($val, true);
  }

  print_r($arr);
}

/*
处理后的数据格式如下,方便处理:

Array
(
[0] => Array
(
[type_code] => 111
[code] => 222
[the_name] => www.chenwei.ws
)

[1] => Array
(
[type_code] => 333
[code] => 444
[the_name] => 把简单做到极致
)
)
*/


早前: [jQ/PHP]使用JS数组储值的两种情况(提交PHP处理)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: