Solved

Returning JSON from PHP

Posted on 2010-09-20
2
416 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 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

19 Experts available now in Live!

Get 1:1 Help Now