Solved

Simple idiots guide to PHP, JQuery and Json

Posted on 2010-09-20
4
1,127 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
[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
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 110

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what should i learn? javascript or typescript? 3 38
How do I add counter to html listbox using jquery 2 26
Problem in javascript 17 31
Boolean 13 25
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 article discusses four methods for overlaying images in a container on a web page
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 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)

730 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