Solved

Simple idiots guide to PHP, JQuery and Json

Posted on 2010-09-20
4
1,119 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 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

16 Experts available now in Live!

Get 1:1 Help Now