从jQuery到JSON到PHP [英] Getting from jQuery to JSON to PHP

查看:72
本文介绍了从jQuery到JSON到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我在答复,我的问题也一直被遗弃,因此我将重做我的问题,以希望有人能看到我的错误并帮助我.我会尽量做到透彻.

My questions keep getting abandoned even though I am replying, so I am going to remake my question in the hopes that someone can see my error and help me out. I will try to be as thorough as possible.

第1步:我有一个名为DivingsLarge的数组,格式如下:

Step 1: I have an array named divisionsLarge in the following form:

divisionsLarge = [{ "car":"Toyota", "color":"blue", "numberTires":"four" }, { "car":"Honda", "color":"red", "numberTires":"four"}, etc. etc. ... ]

(此数据是虚构的,但这是在某个地方出错的过程(也忽略了numberTires被存储为字符串而不是int的事实,它是虚构的人:P)

(This data is fictional, but it's the process that is wrong somewhere (also ignore the fact that numberTires is being stored as a string instead of a int, it's fictional folks :P)

无论如何,我有上述条目中的92个,它们都具有相同的键:汽车,颜色和数字轮胎.

Anyway, I have 92 of the above entries, all with the same keys: car, color, and numberTires.

现在,我使用以下 function 循环遍历数组,以便仅用car& numberTires键:

Now, I go through the array with the following function loop in order to build an array with just car & numberTires key:

var divisions = [];
for (var i = 0; i < divisionsLarge.length; i++) {
    if(divisionsLarge[i].numberTires != "two"){
          var obj = { "car": divisionsLarge[i].car,
                      "numberTires": divisionsLarge[i].numberTires};
          divisions.push(obj);}
    }

好的,在这一点上,我认为一切顺利.如果我在FireBug中使用控制台并输入divisions[0],则会得到一个漂亮的对象,例如,由

Ok, at this point, I THINK everything is good to go. If I use the console in FireBug and type in divisions[0] I get a beautiful object that consists of, for example,

Object { car = "Toyota", numberTires = "four"}

(我认为汽车和numberTires条目周围仍然有"",这正是FireBug显示对象的方式,我可能是错的)

(I think there are still "" around the car & numberTires entries, this is just how FireBug displays the object, I could be wrong)

现在这是我需要帮助的地方.我创建的.ajax查询数量超出了我的预期.我用过JSON.stringified,我没用过JSON.stringified,我用过json_decode(),我刚刚做过print_r($ _ POST)...我做了很多事情,我完全无法做到分析什么会影响到什么,以便诊断可能是什么问题.似乎我的.ajax POSTS可能是错误的,似乎我的PHP可能是错误的.因此,以下是我将不胜感激的问题:

Now here's what I need help with. I've created more .ajax queries than I can count. I've used JSON.stringified, I've not used JSON.stringified, I've used json_decode(), I've just done print_r($_POST)...I've done so many things that I am completely unable to analyze what is affecting what in order to diagnose what the problem might be. It seems my .ajax POSTS might be wrong, it seems my PHP might be wrong. So here are the questions I would GREATLY appreciate being answered:

1)由JavaScript创建的divisions数组是否被视为JSON,或以易于转换为JSON的格式?

1) Is the divisions array being created by the javascript considered JSON, or in a format easily converted to JSON?

2)我的AJAX调用需要是什么?我已经尝试了太多的AJAX调用,所以我不知道什么是对/错.另外,请使用divisions而不是我上面提供的数组的代码段,因为divisions数组是由divisionsLarge中包含的内容动态生成的.

2) What does my AJAX call need to be? I have tried so many AJAX calls that I have NO idea what is considered right/wrong. Also, please use divisions and not the snippet of the array I provided above, as the divisions array is dynamically generated by what's contained in divisionsLarge.

3)我的divisions.php PHP文件需要什么样?现在,它周围有一个HTML骨架,上面带有引用divisionsLarge.jsdivisions.js<script></script>标记[这些是否需要放在一个.js文件中?]我已经看到空白页面和Array()这么长时间了,我什至现在都在怀疑PHP文件的其余部分.

3) What does my divisions.php PHP file need to look like? Right now it has an HTML skeleton around it with <script></script> tags that reference divisionsLarge.js and divisions.js [do these need to be in one .js file?] I have seen blank pages and Array() for so long that I'm even doubting the rest of the PHP file now.

4)例如,如何获取第一个索引的颜色值?这似乎是基本的,但我看到的大多数示例都只是查询仅一个对象的数组,例如echo $_POST["color"],但是我有多个color条目,所以我不确定如何只问第一个.我最想知道这一点,是因为我在测试阵列是否正常工作方面取得了非常不错的成绩-我对print_r($_POST)var_dump($json)失去了全部信心.

4) How do I get, for example, the color value of the first index? This seems rudimentary but most examples that I see are just querying an array of just one object, e.g. echo $_POST["color"], but I have multiple entries of color, so I'm not sure how to just ask for the first one. I would like to know this mostly because I have had such bad success with testing whether the array is working or not - I have lost all faith in print_r($_POST) and var_dump($json).

推荐答案

好的,首先回答您的问题.然后是代码:

Okay, First your questions. Then the code:

1.-除法不视为JSON,它只是一个对象数组.使用JSON.stringify()可以轻松地将所有javascript对象转换为JSON;看下面的代码.

1.- Divisions is not considered JSON, it is just an object array. All javascript objects can easily be turned into JSON using JSON.stringify(); Look at the code below.

2.-查看下面的代码.

2.- Look at the code below.

3.-我不确定您的PHP需要什么样的处理.下面的代码假定您将其发布到另一个页面,您将在其中进行一些处理并输出一些可在complete函数中使用的东西.

3.- I am not sure what kind of processing you need in your PHP. The code below assumes you are posting this to another page, where you will do some processing and output something, that you can use in the complete function.

4.-查看下面的代码.

4.- Look at the code below.

我想这就是你想要做的:

I think this is what you want to do:

JavaScript

Javascript

divisionsLarge = [{ "car":"Toyota", "color":"blue", "numberTires":"four" }, { "car":"Honda", "color":"red", "numberTires":"four"}];

var divisions = [];


for (var i = 0; i < divisionsLarge.length; i++) {
    if(divisionsLarge[i].numberTires != "two"){
          var obj = { "car": divisionsLarge[i].car,
                      "numberTires": divisionsLarge[i].numberTires};
          divisions.push(obj);}
    }

//I am just going to send one of your array elements as a JSON object.
var postData = JSON.stringify(divisions[0]);
var url = "url where you want to post your data";

$.ajax(url, {
    type: "POST",
    data: { "jsonBeingSent" : postData },
    success: function(data) {
      //Do whatever you need to do with your PHP output here. 
      //If you are using something like my php sample then you will be receiving a JSON object(s)

    }
});

现在,在您的PHP上,您可能想要这样的东西:

Now, on your PHP you probably want something like this:

<?php

   //You may want to do some checking about the sender, and so on.
   $receivedData = json_decode($_POST['jsonBeingSent']);

   //You should see something like this if you print_r $receivedData
   // object(stdClass) {
   //      [car] => ...
   //      [numberTires] => ...  
   // }

   //So you could access the car value like this
   echo $receivedData->{'car'};

   //Do your processing and then, if you are using an array or object you can use `json_encode()` to output your data.

?>

希望有帮助.

这篇关于从jQuery到JSON到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆