Solved

Returning JSON from PHP

Posted on 2010-09-20
2
420 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

737 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