Solved

Returning JSON from PHP

Posted on 2010-09-20
2
414 Views
Last Modified: 2012-05-10
I'm having trouble returning a JSON string from a PHP program. I can do it in my "normal" language (Progress) but I can't seem to get the syntax correct in the PHP.

i copied the code below and tried to modify it. I can see values being returned when I create an array and use json_encode() on it but when it comes back to the calling program, it's NULL.

Also, the code in the "if' statement doesn't return the string in JSON format apparently because it doesn't execute any of the code within the callback function.

I can use Progress to do this but I would rather use PHP. Any help would be appreciated.
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require './jsonwrapper.php';

$data = array(
  "foo" => "bar",
  "baz" => "true",
  "thinger" => "thing"
);

//echo json_encode($data);

if ($_REQUEST['id'] == "CAT") {
  echo '[ {optionValue: 0, optionDisplay: "Mark1"}, {optionValue:1, optionDisplay: "Andy"}, {optionValue:2, optionDisplay: "Richard"}]';
} else if ($_REQUEST['id'] == "CEL") {
  echo '[{optionValue:10, optionDisplay: "Remy"}, {optionValue:11, optionDisplay: "Arif"}, {optionValue:12, optionDisplay: "JC"}]';
} else if ($_REQUEST['id'] == "ICE") {
  echo '[{optionValue:20, optionDisplay: "Aidan"}, {optionValue:21, optionDisplay:"Russell"}]';


}?>

/**** jQuery Call ****/
$("select#order_class").change(function()
    {
        
        
      $.getJSON("/cgi-bin/get_shipdates.php",{id: "CAT", ajax: 'true'}, function(j){
        alert(j);
        var options = '';
        for (var i = 0; i < j.length; i++) {
          options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
        }
        $("select#shipdate").html(options);
      })

    });

Open in new window

0
Comment
Question by:Webspeeder
2 Comments
 

Expert Comment

by:BillGeo
Comment Utility
Look at http://php.net/manual/en/function.json-encode.php

I think you will find that one of the comments refers to your question.
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
Try wrapping all of your key / values in double quotes, in both your JS and PHP



//JS

$.getJSON("/cgi-bin/get_shipdates.php",{"id": "CAT", "ajax": "true"}, function(j){



//PHP

echo '[ {"optionValue": "0", "optionDisplay": "Mark1"}, {"optionValue":"1", "optionDisplay": "Andy"}, {"optionValue":"2", "optionDisplay": "Richard"}]';

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now