Solved

Simple idiots guide to PHP, JQuery and Json

Posted on 2010-09-20
4
1,122 Views
Last Modified: 2012-05-10
Hi,
I am familiar with PHP and JQuery(for the basics anyway!). I am looking at ways of speeding up my pages and improving usablity and want to use Json.
Can some one provide me with an example of how I use an array of colors on the server to populate a list in the browser.
colors.php
array('red'=>'Red','green'=>'Green','blue'=>'Blue');

Open in new window


home.htm
<select name='colors' id='colors'>
  <option value=''>Please Choose Color</option>
</select>

Open in new window


Cheers
Joh
0
Comment
Question by:jdav357
  • 2
4 Comments
 
LVL 2

Expert Comment

by:k_romych
ID: 33715241

<select name='colors' id='colors'>
<?php
$colors=array('red'=>'Red','green'=>'Green','blue'=>'Blue');
foreach($colors as $key=>$color){
echo "<option value='{$key}'>$color</option>";
}

?>
</select>

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 33715746
You can find the man pages on JSON at the PHP web site.  Please read the pages linked here:
http://us3.php.net/manual/en/ref.json.php

JSON is a markup or "notation" language, much like XML.  As the name implies ("JavaScript Object Notation") it is useful with JavaScript.  But I would not use JavaScript to build a form from an array on the server. I would just build the form with PHP.  In this case, JSON notation is not a "value-added" part of the process.

See the example to get some familiarity with what JSON does for you.  For my work, its main advantage is that JSON produces smaller data-transfer strings than XML.  I think of JSON or XML as the propellant and the data as the payload.  JSON often requires less propellant to deliver the same payload.

Best, ~Ray
<?php // RAY_JSON_example.php

// SHOW HOW JSON CAN REDUCE THE SIZE OF DATA TRANSFER STRINGS
// SHOW HOW SERIALIZE() MAY NOT BE PERFECT

error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;

// AN XML STRING FOR TEST DATA
$xml = '<?xml version="1.0"?>
<category>
  <parent>
    <child index="1" value="Category 1" />
    <parent>
        <child index="2" value="Subcat1 - lvl1" />
        <parent>
          <child index="3" value="Subcat1 - lvl2" />
          <parent>
              <child index="4" value="Subcat1 - lvl3" />
          </parent>
        </parent>
    </parent>
  </parent>

  <parent>
    <child index="5" value="Category 2" />
    <parent>
        <child index="6" value="Subcat2 - lvl1" />
        <parent>
          <child index="7" value="Subcat2 - lvl2" />
          <parent>
              <child index="8" value="Subcat2 - lvl3" />
          </parent>
        </parent>
    </parent>
  </parent>
</category>';

// MAKE AN OBJECT (GIVES SimpleXMLElement)
$obj = SimpleXML_Load_String($xml);

// VISUALIZE THE OBJECT
var_dump($obj);

// CREATE THE JSON STRING
// MAN PAGE http://us3.php.net/manual/en/function.json-encode.php
$jso = JSON_encode($obj);

// CHECKING THE ERRORS IF PHP >= 5.3
// var_dump( json_last_error() );

// CREATE THE PHP SERIAL STRING
// MAN PAGE http://us3.php.net/manual/en/function.serialize.php
$ser = serialize($obj);

// SHOW THE ORIGINAL STRING LENGTH
$x = strlen($xml);
echo PHP_EOL . "XML REQUIRES $x CHARACTERS";

// VISUALIZE THE JSON
echo PHP_EOL . 'JSON ';
var_dump($jso);

// COMPARE THE STRING LENGTHS
$j = strlen($jso);
$d = $x - $j;
echo "JSON SAVES $d CHARACTERS";

// VISUALIZE THE SERIALIZED STRING
echo PHP_EOL . 'SERIALIZE ';
var_dump($ser);

// COMPARE THE STRING LENGTHS
$s = strlen($ser);
$d = $x - $s;
echo "SERIALIZE() 'SAVES?' $d CHARACTERS";

// DECODE THE JSON STRING (GIVES stdClass)
// MAN PAGE http://us3.php.net/manual/en/function.json-decode.php
$new = json_decode($jso);
echo PHP_EOL;
var_dump($new);

// TRY TO DECODE THE SERIALIZED STRING
$new = unserialize($ser);
echo PHP_EOL;
var_dump($new);

Open in new window

0
 
LVL 2

Author Comment

by:jdav357
ID: 33716899
k_romych, Thanks for your comment, but I did not meanhow to make PHP generate the select box, I wanted PHP to provide the data and JQuery to call the data source so it may generate the select box without the need for a page refresh.
So if a user clicked a tickbox called 'use color' then without page refresh, Jquery would populate the select with a range of colors.

Bear in mind this is merely an example - just to get my head around it.

Ray - I will have a play around with your code and see what happens - thanks!!

But thanks to both for comments!
0
 
LVL 2

Author Closing Comment

by:jdav357
ID: 33835925
Thanks for your help
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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 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 …

766 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