douhuan1979 2015-07-29 14:18 采纳率: 100%
浏览 56

PHP没有获得JQuery serialize()POST参数

I want to send a form with JQuery $.ajax, but I have a problem. It's seems that PHP cannot get serialized $_POST. It's weird because the variable elementiPost is not empty, indeed if I do console.log(parametriPost) the console show me the right content. The weirdest thing is that PHP get parameters that I append manually to parametriPost ($_POST['data_r']) but not those of $(this).serialize()! If I put manually a number in $ore it works fine, so the problem is not the query.

Thank you.

Here's the code:

JQuery

$('form').submit(function(e) {

                e.preventDefault();

                var formId = $(this).attr('id');
                var data = area_get_row_date(formId);

                var parametriPost = $(this).serialize() + '&data_r=' + data;


                $.ajax({                                      
                    url: 'insert_db.php',                  
                    method: 'POST',
                    async: false,
                    data: parametriPost,
                    success: function() {

                         // Success code
                    },
                    error: function(xhr, status, error) {
                        alert("Errore!");
                    }
                });
            });

PHP (insert_db.php)

$data = str_replace('_', '.', $_POST['data_r']);
$ore = $_POST['orelavorateore_2_07_2015'];

    $sql = "INSERT INTO ore_lav
            VALUES (NULL, 134, 4,STR_TO_DATE('" . $data . "', '%d.%m.%Y'), " . $ore . ", 1, 1)";


    $results = api_store_result(api_mysql_query($sql));

This is what parametriPost contains:

lavorati_prenotati=L&periodointegrazione_3_07_2015=on&orelavoratechf_3_07_2015=&orelavorateore_3_07_2015=a&extra_field1_orelavoratechf_3_07_2015=&extra_field1_orelavorateore_3_07_2015=&extra_field2_orelavoratechf_3_07_2015=&extra_field2_orelavorateore_3_07_2015=&orenonlavoratechf_3_07_2015=&orenonlavorateore_3_07_2015=&orenonlavoratetipologia_3_07_2015=L&extra_field1_orenonlavoratechf_3_07_2015=&extra_field1_orenonlavorateore_3_07_2015=&extra_field1_orenonlavoratetipologia_3_07_2015=L&extra_field2_orenonlavoratechf_3_07_2015=&extra_field2_orenonlavorateore_3_07_2015=&extra_field2_orenonlavoratetipologia_3_07_2015=L&orenonpagateore_3_07_2015=&orenonpagatetipologia_3_07_2015=L&extra_field1_orenonpagateore_3_07_2015=&extra_field1_orenonpagatetipologia_3_07_2015=L&extra_field2_orenonpagateore_3_07_2015=&extra_field2_orenonpagatetipologia_3_07_2015=L&orelavoratechf_3_07_2015=&orelavorateore_3_07_2015=&data_r=3_07_2015
  • 写回答

1条回答 默认 最新

  • douchun1961 2015-07-29 15:18
    关注

    You can use this snippet to convert your form data into JSON format :

    $.fn.serializeObject = function()
        {
           var o = {};
           var a = this.serializeArray();
           $.each(a, function() {
               if (o[this.name]) {
                   if (!o[this.name].push) {
                       o[this.name] = [o[this.name]];
                   }
                   o[this.name].push(this.value || '');
               } else {
                   o[this.name] = this.value || '';
               }
           });
           return o;
        };
    
        $("form").submit(function( event ) {
    
            event.preventDefault();
    
            //convert form data to JSON
            var params = $(this).serializeObject();
            //add a 'data_r' field with some data to our JSON
            params.data_r = 'sample data';
    
            $.ajax({
                url: 'app.php',
                type: 'POST',
                data: JSON.stringify(params),
            })
            .done(function(data) {
                console.log(data);
            });
        });
    

    and on the PHP side :

    <?php 
    
        $data = json_decode(file_get_contents('php://input'), false);
    
        print_r($data->data_r);
    
    
     ?>
    

    Now $data is an object and you can access to a specific field :

    $data->data_r
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?