[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1141
  • Last Modified:

Simple idiots guide to PHP, JQuery and Json

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
jdav357
Asked:
jdav357
  • 2
1 Solution
 
k_romychCommented:

<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
 
Ray PaseurCommented:
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
 
jdav357Author Commented:
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
 
jdav357Author Commented:
Thanks for your help
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now