duande1986
2014-02-27 17:49 阅读 33

jQuery从datatables序列化到没有ajax的PHP数组

I have a table using Datatables. I have a setup pretty much like this link in the fact that I have form fields in the table and that they are submitted. I don't want to use ajax, I want to use the submit button.

This is part of my code:

$('#form').submit( function() {
    var sData = $('input', oTable.fnGetNodes()).serialize ();

    console.log(sData);
    $('#form-values').val(sData);

} );

So I'm taking the serialized data and I'm putting it in a hidden input box with the id #form-values. Once I submit, In the server side I can get the serialized data with $_POST['form-values'] but of course... the data is serialized... I would want that data in an array so I can analyze it and insert some of them in a database.

I tried doing unserialize($_POST['form-values']) but it gives me the error Notice: unserialize(): Error at offset 0 of 1098 bytes in...

My serialized data looks like:

comments%5B56%5D=&comments%5B35%5D=&comments%5B12%5D=&comments%5B32%5D=

But I would want it to be:

Array ( [comments] => Array ( [56] => [35] => [12] => [32] => 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dsvtnz6350 dsvtnz6350 2014-02-27 17:55

    You can get just the atributes from a URL using parse_url()

    Once you have that you can use parse_str() to convert them to variables, it works with multidimensional arrays too!

        $str = "first=value&arr[]=foo+bar&arr[]=baz";
    
        parse_str($str, $output);
        echo $output['first'];  // value
        echo $output['arr'][0]; // foo bar
        echo $output['arr'][1]; // baz
    
    点赞 评论 复制链接分享
  • douyao3895 douyao3895 2014-02-27 17:53

    Try this:

    $params = array();
    parse_str($_GET, $params);
    
    点赞 评论 复制链接分享
  • dongqian5639 dongqian5639 2014-02-27 17:59

    Why not to use parseJSON jQuery function?

    点赞 评论 复制链接分享

相关推荐