0

I am trying to use ajax and json to pull arrays from this php. I am kind of familiar with the ajax on the front end, but i need to put each of these php arrays into a javascript array so i can make a list. Any help would be great. Thanks

<?php

    header('Content-Type: application/json');

    echo '{}';


    function getOptions($selection) {

        switch ($selection) {
            case 'colors':
                $arr = array(
                    0 => 'Red',
                    1 => 'Orange',
                    2 => 'Yellow',
                    3 => 'Green',
                    4 => 'Blue',
                    5 => 'Indigo',
                    6 => 'Violet'
                );
                break;
            case 'dogs':
                $arr = array(
                    0 => 'Labrador Retriever',
                    1 => 'Yorkshire Terrier',
                    2 => 'German Shepherd',
                    3 => 'Golden Retriever',
                    4 => 'Beagle',
                    5 => 'Dachshund',
                    6 => 'Boxer',
                    7 => 'Poodle',
                    8 => 'Shih Tzu',
                    9 => 'Miniature Schnauzer'
                );
                break;
            case 'fruits':
                $arr = array(
                    0 => 'Apples',
                    1 => 'Bananas',
                    2 => 'Cantaloupe',
                    3 => 'Grapefruit',
                    4 => 'Papaya',
                    5 => 'Mango',
                    5 => 'Strawberries',
                    6 => 'Watermelon'
                );
                break;
            case 'plants':
                $arr = array(
                    0 => 'Norfolk Island Pine',
                    1 => 'Peperomia',
                    2 => 'Grape Ivy',
                    3 => 'Fiddleleaf Fig',
                    4 => 'Snake Plant',
                    5 => 'English Ivy',
                    6 => 'Spider Plant',
                    7 => 'Hoya',
                    8 => 'Green Dracaena',
                    9 => 'Pothos'
                );
                break;
            default:
                $arr = array();
        }

        return $arr;

    }
echo json_encode($arr);

?>

Here is the ajax call

$.ajax({
                type: 'POST',
                url: 'ajax.php',
                data: 'id=testdata',
                dataType: 'json',
                cache: false,
                success: function(result) {
                    var numbers = result
                    //not sure what to put here to pull separate arrays from the php
                    //should it be something like var colors = result.colors??
                    }           
                }
            });
1
  • 2
    You're not calling getOptions anywhere, but trying to echo its internal result variable? Commented Jun 3, 2013 at 16:34

4 Answers 4

1

Modify your PHP like so:

Remove the echo '{}'; line, and replace the line echo json_encode($arr); with

echo json_encode(array(
  'colors' => getSelections('colors'),
  'dogs'   => getSelections('dogs'),
  'fruits' => getSelections('fruits'),
  'plants' => getSelections('plants')
);

Then in your Javascript you can use result.colors[3], result.dogs[5], etc.

Sign up to request clarification or add additional context in comments.

Comments

0

Why dont you just use json_encode of the array : http://php.net/manual/en/function.json-encode.php

Comments

0

you can put them inside objects and then json_encode, then you can use jquery with dataType:'json' and parse them back on the browser

3 Comments

I got that part, but how do i call on each array individually?
@user1873632 you don't need to encode each single array, you just need to encode the one you need.
in the json or in php? if it's on json just go to result.array1.elem and in php $array[0] or $array[1]
0

If you want to return the whole structure as

{
  "colors": ["Red", "Orange", ...],
  "dogs": ["Lab", "Yorkie", ...], 
  ...
}

Then you can use

<?php
header('Content-Type: application/json');
$map = array(
    'colors' => array('Red', 'Orange', 'Yellow', 'Green', 'Blue'),
    'dogs' => array('Labrador Retriever', 'Yorkshire Terrier')
);
echo json_encode($map);

// js
$.ajax({
   type: 'POST',
   url: 'ajax.php',
   data: 'id=testdata',
   dataType: 'json',
   cache: false,
   success: function(result) {
        var numbers = result
        var colors = result.colors; // colors[0], colors[2]
        var dogs = results.dogs;            
   });

If you want to pass the key into the AJAX call, use the following

<?php
header('Content-Type: application/json');
$map = array(
    'colors' => array('Red', 'Orange', 'Yellow', 'Green', 'Blue'),
    'dogs' => array('Labrador Retriever', 'Yorkshire Terrier')
);
echo json_encode($map[$_POST['field']]);

// js
$.ajax({
   type: 'POST',
   url: 'ajax.php',
   data: 'field=colors',
   dataType: 'json',
   success: function(colors) {
        alert(colors[0] + colors[1] );
   });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.