jquery序列化和$.post,并在服务器端接收不完整的数组


jquery serialize and $.post and receiving incomplete array on serverside

jquery:

$("#btnSaveForm").click(function(){
...
$.post('../optional/setup/processItemcn.php', $("#FormTemplate").serialize(), function(data) {
     alert(data);
});
...
});

php的形式:

<form id="FormTemplate" class="FormTemplate">
  <input type="text" name="kode_item[]" value="1" />
  <input type="text" name="kode_item[]" value="2" />
  <input type="text" name="kode_item[]" value="3" />
  .
  .
  <input type="text" name="kode_item[]" value="197" />
</form>

服务器端的php:

$ID = $_POST["kode_item"];
$count = count($ID);   
echo "$count";

我正在尝试使用jquerypost和serialize将表单中的所有数组数据发送到服务器端。我有kode_item=197个项目的total-items数组,但在将其发送到服务器端,然后我从服务器端检查并回显它之后,结果为$count=167个项目。这就是为什么我不能更新与kode_item>167相关的数据,因为它不会在服务器端处理。有人有什么想法吗?

尝试检查php.ini 中的设置是否正确

; Maximum size of POST data that PHP will accept.
post_max_size = 8M

我也遇到了这个问题,但没有找到解决方案,在阅读jQuery文档后,我有了一个想法。

您可以使用.serializeArray(),而不是仅使用.serialize()并播放php.ini文件。这是我在项目中使用的代码。

jQuery('.modal').on('click','.saveFeature',function(){ 
        var form = jQuery('#featureForm').serializeArray();
        jQuery.ajaxSetup({
            data:{
                csrf_name:jQuery('input[name="csrf_name"]').val()
            }
        });
        var url = "<?php echo 'product/features/'.$pid;?>";
        jQuery.post(url,{form}, function(msg){
            alert(msg);
        } );
    });

提交表格后,您将获得以下格式的数据。

Array
(
[0] => Array
    (
        [name] => csrf_name
        [value] => 3802b11296d42e602533bf266bb800bd
    )
[1] => Array
    (
        [name] => title
        [value] => kjhkjh
    )
[2] => Array
    (
        [name] => title_icon
        [value] => fa fa-heart
    )
[3] => Array
    (
        [name] => icon_color
        [value] => #a81616
    )
[4] => Array
    (
        [name] => icon_bg_color
        [value] => #662f2f
    )
[5] => Array
    (
        [name] => description
        [value] => testb
    )
[6] => Array
    (
        [name] => box_bg_color
        [value] => #bd2222
    )
[7] => Array
    (
        [name] => box_bttm_bg
        [value] => #a61e1e
    )
)

在后台处理post数据时,我使用的是CodeIgniter框架。以key=>value格式组织发布数据,以便轻松存储数据。

$arry = $this->input->post('form');
    $post = [];
    //check if this is not empty and is array
    if(!empty($arry) && is_array($arry)){
        //iterate over post data and organize array in key value format.
        foreach($arry as $ar) 
        {
            $post[$ar['name']] = $ar['value'];
        }
    }
    $data = [
        'pid' => $pid,
        'title' => $post['title'],
        'icon' => $post['title_icon'],
        'icon_color' => $post['icon_color'],
        'icon_bg_color' => $post['icon_bg_color'],
        'description' => $post['description'],
        'box_bg_color' => $post['box_bg_color'],
        'box_bttm_color' => $post['box_bttm_bg'],
    ];
    $this->db->insert(TBL_PRODUCT_FEATURES, $data);

如果我错了,请纠正我;我刚刚分享了我解决这个问题的想法。

该解决方案来自我的网络托管技术人员。

从窗体发送到服务器端的数组数据太长,则。。suhosin限制将要发送的数组数据。

这种方式对我有用。将这两行添加到php.ini:

suhosin.post.max_vars = 1500
suhosin.request.max_vars = 1500