简体   繁体   English

如何将json对象值传递给php dinamically?

[英]How to pass json object values to php dinamically?

Hi I'm newbie in JSON and Ajax and my question is probably quite stupid but when learning, also the stupid questions are fundamental. 嗨,我是JSON和Ajax的新手,我的问题可能很愚蠢,但在学习时,愚蠢的问题也是根本。

I need to pass two parameters via Ajax (giorno and periodo), 我需要通过Ajax(giorno和periodo)传递两个参数,

for example 'giorno' = 2017-05-10 and 'periodo' = 2: 例如'giorno'= 2017-05-10和'periodo'= 2:

$.ajax({
    type:'POST',
    data: JSON.stringify({
    giorno: $('#dataselezionata').val(),
    periodo: $('input:radio[name=periodo]:checked').val()
    }),
    contentType: "application/json; charset=utf-8",
    dataType : 'json',
    url:'http://www.rinnovipatenti.com/index2.php?a=prenotazione' 
    });

The JSON object passed perfectly and the result in Firebug Console is: JSON对象完美传递,Firebug控制台中的结果是:

{"giorno":"2017-05-10","periodo":"2"}

If I try to manually copy and paste the object on the php page like this: 如果我尝试手动复制并粘贴php页面上的对象,如下所示:

<?
$json       = '{"giorno":"2017-05-10","periodo":"2"}'; //pasted manually

$json       = json_decode($json, TRUE);

$giorno = $json['giorno'];
$periodo    = $json['periodo'];

echo"$giorno"; //2017-05-10
echo"$periodo"; //2
?>

the two echoes show me the values. 这两个回声向我展示了价值观。 OK! 好!

My problem start and stop here . 我的问题在这里开始和停止 I'm not able to bring the JSON object to be decoded. 我无法将JSON对象解码。

I'm quite sure is a stupid solution but I don't know how to do that. 我很确定这是一个愚蠢的解决方案,但我不知道该怎么做。

I need to create a function that wrap the Ajax call and then call the function in json_decode?? 我需要创建一个包装Ajax调用的函数,然后在json_decode中调用该函数?

PS I also tried to simply get the values with "$_POST['giorno']" etc.. instead of using JSON but without success. PS我也尝试用“$ _POST ['giorno']”等来获取值,而不是使用JSON但没有成功。

Can someone help me please? 有人能帮助我吗? Thank you for your patience. 感谢您的耐心等待。

UPDATE 10/05/2017 更新10/05/2017

Hi I've followed your suggestions so I tried one time more to simplify the code like this: 嗨我已经按照你的建议,所以我尝试了一次更简单的代码,如下所示:

$('input:radio[name=periodo]').change(function() {

var giorno = document.getElementById("dataselezionata").value; // from datepicker
var periodo = $('input:radio[name=periodo]:checked').val(); // from radio button

var post_data = ("giorno="+giorno+"&periodo="+periodo);

$.ajax({
    type:'GET',
    data: post_data,
    url:"http://www.rinnovipatenti.com/prenota/prenotazione.php",
    });

if (this.value == '1') {
            $('#orario').show();
            $('#orari-mattina').show();
            $('#orari-pomeriggio').hide();
            }
else if (this.value == '2') {
            $('#orario').show();
            $('#orari-pomeriggio').show();
            $('#orari-mattina').hide();
        }

using GET method instead of the POST one and in the PHP page prenotazione.php the code now is: 使用GET方法而不是POST方法,在PHP页面prenotazione.php中 ,代码现在是:

<?
$giorno = $_GET['giorno'];
$periodo    = $_GET['periodo'];   

echo"$giorno";
echo"$periodo";
?>

In Firebug console the parameters are ok 在Firebug控制台中,参数正常

giorno 2017-05-10
periodo 2

the formatted link is: 格式化的链接是:

http://www.rinnovipatenti.com/prenota/prenotazione.php?giorno=2017-05-10&periodo=2

the html console preview works correctly but the page don't. html控制台预览正常工作,但页面没有。 I'm in the corner! 我在角落里! Is very strange. 很奇怪。 I have only one doubt: can I send data by GET/POST method to the same page where the data are collected? 我只有一个疑问:我可以通过GET / POST方法将数据发送到收集数据的同一页面吗? In other word can the page foo.php send data to foo.php like a cycle without refresh? 换句话说,页面foo.php可以将数据发送到foo.php,就像没有刷新的循环一样? And the ajax call could be wrapped inside the .change function or must be outside? 并且ajax调用可以包含在.change函数中,还是必须在外面?

$.post( "http://www.rinnovipatenti.com/index2.php?a=prenotazione", {
    giorno: $('#dataselezionata').val(),
    periodo: $('input:radio[name=periodo]:checked').val()
    } );

you do not need to stringify your JSON 你不需要将你的JSON字符串化

on PHP side you just use 在PHP方面,你只需使用

$giorno = $_POST['giorno'];
$periodo    = $_POST['periodo'];

to get the values 获得价值

you can use the following function. 你可以使用以下功能。 you have it already. 你已经拥有它了。 it worked fine for me. 它对我来说很好。

$('input:radio[name=periodo]').change(function() {

    var giorno = '2017-05-15';
    var periodo = '2';

    $.post( 'http://www.rinnovipatenti.com/index2.php?a=prenotazione', {
        giorno: giorno,
        periodo: periodo
        });
    /*....*/
}); 

Method 2 方法2

You don't have to stringify the JSON object 您不必将JSON对象进行字符串化

$.ajax({
    type:'POST',
    data: {
    giorno: $('#dataselezionata').val(),
    periodo: $('input:radio[name=periodo]:checked').val()
    },
    contentType: "application/json; charset=utf-8",
    dataType : 'json',
    url:'http://www.rinnovipatenti.com/index2.php?a=prenotazione' 
    });

So you php code will be like this 所以你的PHP代码将是这样的

<?


$giorno = $_POST['giorno'];
$periodo    = $_POST['periodo'];

echo"$giorno"; //2017-05-10
echo"$periodo"; //2
?>

Method 2 方法2

If you want to stringify the JSON object then put it in a key 如果要对JSON对象进行字符串化,则将其放入密钥中

$.ajax({
    type:'POST',
    data: 'data='+JSON.stringify({
    giorno: $('#dataselezionata').val(),
    periodo: $('input:radio[name=periodo]:checked').val()
    }),
    contentType: "application/json; charset=utf-8",
    dataType : 'json',
    url:'http://www.rinnovipatenti.com/index2.php?a=prenotazione' 
    });

So you php code will be like this 所以你的PHP代码将是这样的

<?
$json       = $_POST['data'];

$json       = json_decode($json, TRUE);

$giorno = $json['giorno'];
$periodo    = $json['periodo'];

echo"$giorno"; //2017-05-10
echo"$periodo"; //2
?>

When you send application/json payload to php to access that payload use: 当您将application/json有效负载发送到php以访问该有效负载时:

$json = json_decode(file_get_contents('php://input'), TRUE);

As mentioned in other comments and answers if you stay with $.ajax default form encoding and don't json stringify the data then use $_POST 正如在其他评论和答案中提到的那样,如果你使用$.ajax默认表格编码并且不用json字符串化数据然后使用$_POST

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

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