简体   繁体   中英

How to Read JSON Post Object from PHP

I have a problem with "JSON, JQuery, Ajax, JavaScript and PHP". I have code to save data. This code send data(JSON) via JQuery Ajax to proses.php. The code for save data shown below :



    $("#save").click(function(){
        var id_promotion_code = $("#id_promotion_code").val();
        var i=0;
        var y=0;

        var datarule = {
            rule: []
        };

        $('#tablerule tr').each(function() {
            if(y!=0)
            {
                var id_pricing_rule = $(this).find("td").eq(0).html();              
                var date_book_start = $(this).find("td").eq(3).html();              
                var date_book_end = $(this).find("td").eq(4).html();   
                var date_book_no_end = 0;
                if(date_book_end=="NO END")
                {
                    date_book_no_end = 1;
                    date_book_end = $(this).find("td").eq(3).html();
                }
                var date_reservation_start = $(this).find("td").eq(5).html();               
                var date_reservation_end = $(this).find("td").eq(6).html();             
                var date_reservation_no_end = 0;
                if(date_reservation_end=="NO END")
                {
                    date_reservation_no_end = 1;
                    date_reservation_end = $(this).find("td").eq(5).html();
                }
                datarule.rule.push({ 
                    "id_promotion_code" : id_promotion_code,
                    "id_pricing_rule" : id_pricing_rule,
                    "date_book_start" : date_book_start,
                    "date_book_end" : date_book_end, 
                    "date_book_no_end" : date_book_no_end, 
                    "date_reservation_start" : date_reservation_start, 
                    "date_reservation_end" : date_reservation_end, 
                    "date_reservation_no_end" : date_reservation_no_end
                });
                i++;
            }
            y++;
        });
        $.ajax({
            type:"POST",
            url:"proses.php",
            data:"aksi=tambahrule&datarule=" + datarule,
            success: function(data){
                alert("Sukses " + data);
            },
            error: function(){
                alert("Error" + data);
            }
        });
    });

And the code in proses.php shown below :



    if($aksi=='tambahrule'){
            $datarule = $_POST['datarule'];
            $jsone = json_decode($datarule, true);
            print_r($jsone);
    }

But i can't get the json data with proses.php code. Please help me how to read json object that send via jquery ajax with php? Actually i want to looping the json for get the data.

------------------------MY NEW EDIT-----------------------------

Thanks for your response... I already modify my code. And running but not well yet. This is the response when i check using Firebug :



    Array
(
    [rule] => Array
        (
            [0] => Array
                (
                    [id_promotion_code] => 
                    [id_pricing_rule] => BN2
                    [date_book_start] => 2012-03-01
                    [date_book_end] => 2012-03-01
                    [date_book_no_end] => 1
                    [date_reservation_start] => 2012-03-09
                    [date_reservation_end] => 2012-03-09
                    [date_reservation_no_end] => 1
                )

            [1] => Array
                (
                    [id_promotion_code] => 
                    [id_pricing_rule] => EB10%
                    [date_book_start] => 2012-03-15
                    [date_book_end] => 2012-03-15
                    [date_book_no_end] => 1
                    [date_reservation_start] => 2012-03-31
                    [date_reservation_end] => 2012-03-31
                    [date_reservation_no_end] => 1
                )
        )
)

And this is the PHP code for get the data :



        $datarule = $_POST;
        $rulenya="";
    foreach($datarule->rule as $doc) 
    {
        $rulenya=$rulenya.$doc->id_pricing_rule;
    }
    print_r($datarule);

But get the error. My question is? 1. The data is in Array, should i change to Object? And how? 2. How can i get that data in PHP?

I think you should send the data in another way:

data: { aksi: "tambahrule", datarule:  datarule},

as datarule is a complex object and can't be appended to a querystring. Serverside your code should be ok

尝试在ajax调用中使用dataType: 'json',

You don't json_decode. You can try it;

PHP Code:

$datarule = $_POST['datarule'];
$datarule = implode(',', $datarule);
print_r($datarule);

$datarule is a array

The answer of Nicola Peluchetti is correct, datarule is NOT a querystring, you need to send it as he explained.

Furthermore, having your data as an array is fine. But you need to alter your server code to handle it as such:

$datarule = $_POST['datarule'];
$rulenya = '';
foreach ($datarule['rule'] as $doc) {
    $rulenya .= $doc['id_pricing_rule'];
}

or something along those lines...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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