Solved

Returning JSON from PHP

Posted on 2010-09-20
2
419 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
ID: 33721506
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 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 33721627
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 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