【问题标题】:how to save serialize array of javascript value store in PHP on SUBMIT如何在提交时在 PHP 中保存 javascript 值存储的序列化数组
【发布时间】:2015-04-27 09:16:13
【问题描述】:

我是第一次使用 jquery 数据表,所以现在我做了这样的表

一切正常。现在我在下面的 javascript 代码中使用它

    $(document).ready(function() {
        $('#example').dataTable( 
        {
            "pageLength": 50,
            'sDom': 'l' ,
        } );
        $('#example tfoot th').each( function () 
        {
            var title = $('#example thead th').eq( $(this).index() ).text();
            $(this).html( '<input type="text" style="width:100%;" id="munna_'+title+'" placeholder="Search '+title+'" />' );
        });


        var table = $('#example').DataTable();
        table.columns().every( function () 
         {
        var that = this;
        $( 'input', this.footer() ).on( 'keyup change', function () {
        that
        .search( this.value )
        .draw();
        });
        });


$('#munna_button').click( function()
   {
         var data = table.$('input, select').serialize();
        alert(JSON.stringify(data))
        return false;
    });
});

现在此代码在 rate_14=67&rate_15=87&rate_67=88 等中返回类似这样的值,

现在我不知道如何将其存储在 SUBMIT 中。通常在提交时我会像这样得到 $_POST['name'],现在请有人帮我解决这个问题。

【问题讨论】:

  • 你需要调用ajax。在需要接受 php 中的 post 请求的地方添加 ajax url。 $.ajax({url:"", type:"POST", data:"serializeData"});
  • @Munna 尝试 $.ajax() 访问api.jquery.com/jquery.ajax
  • 但是可以输入超过 20 个值,所以当我在 ajax 中输入时可能需要更多时间,这就是我在提交时询问的原因

标签: javascript php jquery serializearray


【解决方案1】:

只需使用ajax发送信息:

$('#munna_button').click( function(){
      var data = table.$('input, select').serialize();
      $.post('url_link_here', data, function(returnData){
        //success -- do stuff with returnData if there is any

      }).fail(function(){
        //failed

      });
 });

【讨论】:

  • 会有超过 100 条记录,所以 rate_14=67&rate_15=87&rate_67=88... 会持续这么久??它不会影响和变慢吗?
  • @Munna 为什么会很慢?无论您是实际提交所有信息并刷新页面还是通过 ajax 发送,您的互联网速度和服务器容量都不会改变。会一样的。
【解决方案2】:

您可以将序列化数据存储到添加到表单的隐藏变量中。 并在 java 脚本中使用类似这样的方法将值保存在隐藏字段中

$('#serialize_data').val(JSON.stringify(data));

表单提交后,会在里面找到值

$_POST['serialize_data']

如果不想提交表单,可以使用ajax发送数据到.php脚本 希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-05
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2017-03-08
    • 1970-01-01
    • 2020-08-29
    相关资源
    最近更新 更多