简体   繁体   English

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

[英]PHP doesn't get JQuery serialize() POST parameters

I want to send a form with JQuery $.ajax , but I have a problem. 我想使用JQuery $.ajax发送表单,但是我遇到了问题。 It's seems that PHP cannot get serialized $_POST . 似乎PHP无法序列化$_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. 这很奇怪,因为变量elementiPost不为空,实际上,如果我执行console.log(parametriPost) ,控制台会向我显示正确的内容。 The weirdest thing is that PHP get parameters that I append manually to parametriPost ($_POST['data_r']) but not those of $(this).serialize() ! 最奇怪的是,PHP获取了我手动附加到parametriPost ($_POST['data_r'])而不是$(this).serialize() If I put manually a number in $ore it works fine, so the problem is not the query. 如果我在$ ore中手动输入一个数字,它可以正常工作,因此问题不是查询。

Thank you. 谢谢。

Here's the code: 这是代码:

JQuery 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) 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: 这是parametriPost包含的内容:

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

You can use this snippet to convert your form data into JSON format : 您可以使用此代码段将表单数据转换为JSON格式:

$.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方面:

<?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->data_r

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM