0

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.

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. ... ]

(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)

Anyway, I have 92 of the above entries, all with the same keys: car, color, and 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);}
    }

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"}

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

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) Is the divisions array being created by the javascript considered JSON, or in a format easily converted to JSON?

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) 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) 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).

Kris
  • 201
  • 1
  • 8
  • One helpful point: I think you might want to look at http://stackoverflow.com/questions/206988/how-do-i-unset-an-element-in-an-array-in-javascript to see how to easily unset the element of your array instead of building it again. – Aaron Saray Feb 26 '13 at 02:56
  • I was going to make a comment about how I know there is probably a much easier way to split the array with built-in methods, but that monster up there is something I pored my heart and soul into refining haha. Now, if it's returning something that's not usable or easily convertible to JSON, then I'll toss it at a moment's notice ^^ – Kris Feb 26 '13 at 03:01
  • Honestly, I'm not sure yet - but the goal is to remove any unknowns. I'd rather just remove elements from a known good json array - than rebuild one. You might comment on if you swap that link's solution in - if its still giving you grief. – Aaron Saray Feb 26 '13 at 03:04
  • What do you want to send to PHP? The whole divisions or just one of them? – Ares Feb 26 '13 at 03:31
  • I actually need PHP to iterate some urls to gather some data server-side, so I'm only at the point in that process where I'm trying to get PHP the urls that I want to iterate on... (the urls are one of the key value pairs in the original array, and then it's just the id and url in the divisions array This has been quite the learning process so far, but it's been pretty painful. – Kris Feb 26 '13 at 03:36
  • The worst part about this is that I can't upvote comments since I only have 6 rep, so my posts get buried super quick :( – Kris Feb 26 '13 at 03:44

1 Answers1

1

Okay, First your questions. Then the code:

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.- Look at the code below.

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.- Look at the code below.

I think this is what you want to do:

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)

    }
});

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.

?>

Hope that helps.

Ares
  • 5,905
  • 3
  • 35
  • 51
  • Used your ajax, replaced `url where you want to post your data` with `divisions.php`, used your php and tried to `print_r($receivedData);`, all I get is a blank screen. FireBug doesn't even register the POST. – Kris Feb 26 '13 at 04:32