我正在使用DOM CreateElement((动态创建html输入元素
function createHtmlElem( elName , attri ){
var el = document.createElement(elName);
for( var key in attri ){
val = attri[key];
el.setAttribute( key , val );
}
return el;
}
我想使用createHtmlElem( 'input' , {"name":"usernames[]"} )
创建以下html元素,它很有效,我的意思是它在页面上创建html元素。
<input type="text" name="usernames[]" />
问题出在服务器端我使用jquery.serialize((将表单序列化并发布,在服务器端
预期是
[usernames] => Array
(
[0] => andrew
[1] => arul
)
但我正在成为
[usernames[]] => 'andrew'
[usernames[]] => 'arul'
我完全困惑了,当你通过放置原始html静态地创建输入数组元素并发布它时,这是非常不可能的。
我创建了一个post_procrocessing函数来处理接收到的数组。希望它对某人有用。
/**
* [create_array description]
* Convert Array
* arr = [
* 'username_1'=>'John Deo',
* 'address_1'=>'wuhan',
* 'pin_1'=>'560093',
* 'username_2'=>'Dennis',
* 'address_2'=>'Hubei',
* 'pin_2'=>'605110'
* ];
*
* arr = [
* [1] => [
* 'username' => 'John Deo',
* 'address' => 'wuhan',
* 'pin' => '560093',
* ],
* [2] => [
* 'username' => 'Napoleon',
* 'address' => 'Karnataka',
* 'pin' => '605110'
* ]
* ]
* @param [type] $inp [description]
* @param integer $nitems [No of Set of items, for this eg nitems = 2]
* @return [type] [Rearrange array as required]
*/
function rearrange_array( $input_arr , $nitems){
//unset( $inp['donor_id'] );
$n = count( $input_arr );
$i = 0;
//nitems is the number of items of objects or books
for($i = 0; $i < $nitems ; $i++){
$tmp = [];
foreach( $input_arr as $k => $v ){
$new_key = preg_replace("/_[0-9]/",'', $k);// getrid of the underscore and the number behind.
$tmp[$new_key]=$input_arr[$new_key . '_' . $i];
}
$output_arr[$i]= $tmp;
}
return $output_arr;
}