Solved

Simple idiots guide to PHP, JQuery and Json

Posted on 2010-09-20
4
1,115 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
Comment Utility

<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 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your help
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to count occurrences of each item in an array.
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 …

771 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

9 Experts available now in Live!

Get 1:1 Help Now