[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Returning JSON from PHP

Posted on 2010-09-20
2
Medium Priority
?
423 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 44

Accepted Solution

by:
Chris Stanyon earned 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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 …
Suggested Courses

656 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