简体   繁体   English

使用JQuery将JSON数据从Javascript传递到PHP

[英]Passing json data from Javascript to PHP using JQuery

I want to pass json data from my Javascript script to the PHP one and I am kinda struggling with it. 我想将JSON数据从我的Javascript脚本传递到PHP脚本,而我对此却有些挣扎。

First here is the code: 首先是代码:

var d = new Date(),
    DayOfWeek = d.getDay(),
    Actual_Day = d.getDate(),
    Month = d.getMonth() + 1;
if (Month < 10) {
    Month = '0' + Month;
}
var Year = d.getFullYear();
var Date = Year.toString() + Month.toString() + Day.toString();
var Hour = d.getHours(),
    Minutes = d.getMinutes();

var data = {
    "YearKey": Year,
    "MonthKey": Month,
    "Actual_DayKey": Actual_Day,
    "DayOfWeekKey": DayOfWeek,
    "DateKey": Date,
    "HourKey": Hour,
    "MinutesKey": Minutes

};

jQuery.ajax({
    type: "POST",
    url: 'index.php',
    type: 'json',
    data: data,
    success: function(data) {
        console.log('success!');

    }
});

In my PHP script, I've tried to be sure that I have a value but it seems to not be the case: 在我的PHP脚本中,我试图确保自己有一个值,但事实并非如此:

if (isset($_POST['YearKey'])) {echo $_POST['YearKey']; exit();}

Any solutions, advices would be appreciated! 任何解决方案,建议将不胜感激!

In your jQuery , type:ajax means you prepare the function to an ajax response 在您的jQuery中, type:ajax意味着您准备了该函数以应对 ajax 响应

in your case you need to send a JSON 在您的情况下,您需要发送一个JSON

put that line in your client code 将该行放入您的客户代码中

data: {data:JSON.stringify(data)}
// will send the JSON string with the request in a parameter called data

on your server decode it 在您的服务器上解码

<?php
$data = json_decode($_POST['data']);

It's actually quite simple. 实际上很简单。 In order to pass variables through to your PHP file from your AJAX request, you need to edit the URL portion of the request. 为了将变量从AJAX请求传递到PHP文件,您需要编辑请求的URL部分。 For example, if your url looks like this: 例如,如果您的网址如下所示:

url: "name_of_file.php/?value=VARIABLE-STUFF&more=VARIABLE-STUFF&etc=etc"

The variable values of value, more, and etc will be accessible in your PHP file. value,more等的变量值将在您的PHP文件中访问。 Then use: 然后使用:

$variable_name = $_GET['value'];

to store the variable 'value' into 'variable_name' for use in PHP file, and etc for any other variables you pass to the PHP file. 将变量“值”存储到“ variable_name”中,以便在PHP文件中使用,对于传递给PHP文件的任何其他变量,等等。 For your case, I would suggest just passing the entire data object into the PHP file and working from there. 对于您的情况,我建议仅将整个数据对象传递到PHP文件中并从那里开始工作。

url: "index.php/?object=" + data

Then in PHP file do: 然后在PHP文件中执行以下操作:

$object_name = $_GET['object'];

Now, your data object in your HTML file will be usable in your PHP file under the variable $object_name. 现在,HTML文件中的数据对象将在PHP文件中的变量$ object_name下可用。

file_get_contents(php://input) 

gets the raw POST data like JSON object that cannot be decoded to $_POST. 获取无法解码为$ _POST的原始POST数据(如JSON对象)。

<?php
$data = var_dump(file_get_content('php://input'));

you need to check all posted data having values or not 您需要检查所有发布的数据是否具有值

so please check with console.log(data); 所以请检查console.log(data); .

i think it may help you . 我认为这可能对您有帮助。

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

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