Javascript, JSON and PHP

March 29, 2009

JSON data is commonly used as a simple way to send data back to client side javascript from the server. For example, a PHP script may output the following JSON to confirm that an action was taken:

{success: true, message: "It worked!"}

or in the case of failure:

{success: fase, message: "It didn't work!"}

Below is a basic javascript callback function to process the JSON data, displaying the returned message:

function callback(data) {
    if(data.success) alert(data.message);
    else alert('ERROR:' + data.message);
}

Sometimes it’s actually useful to send back javascript code to in the JSON data, to be run on the client. For example, if an Ajax request is made that updates some account information we could return javascript code to update several parts of the page with the new information. Here’s PHP code that outputs JSON data which includes javascript code:

$response = array('success' => 'true',
    'code' => "jQuery('#id').html('Success!');");
echo json_encode($response);
// Outputs {success: true, code: "jQuery('#id').html('Success!');"}

Note how in the returned JSON data “code” is actually a string, so it won’t be possible to run the code simply by doing “data.code()” on the client. Instead we must explicitly tell javascript to evaluate the string as code. Below is an example of how we can do this using eval:

function callback(data) {
    if(data.success && data.code) eval(data.code);
}

If we wanted to update the page with HTML more complicate the the simple “Success!” message in the previous example we must be careful to escape any quotes in the HTML correctly. If we wanted to set the content to: “Javascript”, “json”, “PHP” then we’d need to do the following:

$response = array('success' => 'true',
    'code' => "jQuery('#id').html('\"Javascript\", \"json\", \"PHP"');");
echo json_encode($response);

And It’s easy to imagine a far more complicated example. The code becomes less readable, and if you accidentally forget to escape one of the quotes, or escape of the quotes that shouldn’t be escaped, then the whole thing breaks. A nice alternative is ty store the HTML as a separate JSON property and then to reference it, avoiding the need to escape the quotes and making the code easier to read:

$response = array('success' => 'true',
    'html' => '"Javascript", "json", "PHP"',
    'code' => "jQuery('#id').html(this.html);");
echo json_encode($response);

However, to make the “this.html” reference work the clients side callback function will need to changed, as eval() runs the code in global scope. This means that “this.html” will refer to a global html variable rather than the one in our JSON data. We can get around this problem by using the apply function as follows:

function callback(data) {
    if(data.success && data.code) {
        var f = new Function(data.code);
        f.apply(data);
    }
}

And here’s a complete example which uses jQuery to perform the Ajax call:

jQuery.get('update.php', {} function(data) {
    if(data.success && data.code) {
        var f = new Function(data.code);
        f.apply(data);
    }, 'json');

Top 10 Freelance Developer Job Sites

March 10, 2009

Whether your a full time freelancer or someone who does projects on the side for a bit of extra cash the web is a great place to find new clients and exciting projects. Below is a list of ten of the best freelance development job sites on the Internet:

1. All Dev Jobs
Mainly web development projects.

2. Authentic Jobs
A fairly active job board that attracts some great project postings.

3. Freelance Switch
The freelance switch job board is very active, and contains a wide range of development jobs. There is a catch though: It costs $7 a month to reply to any of the postings.

4. Javascript Ninja Jobs
This is a job board for freelance javascript jobs from John Resig, the author of the jQuery library.

5. MetaFilter Jobs
Freelance development projects, mostly consisting of web and mobile application development.

6. No Agencies Please
Primarily a site for UK based work, but there are lots of remote projects. Includes a whole range of different development projects, and isn’t just limited to web development.

7. Programmer Meet Designer
A site that aims to introduce programmers to designers. Many of the projects that get listed are for skills exchanges (eg. you code my site I’ll design yours. There are occasionally paid jobs though, so it’s worth checking the site every so often.

8. Search Web Jobs
Lots of web development jobs, primarily involving CSS, HTML, PHP and MySQL.

9. Smashing Jobs
The job board of the online web development magazine.

10. Web Directions Jobs
Web development jobs. Mostly PHP and Javascript.

So that’s 10 of the best places to find freelance development work online. Checking those 10 sites every for new projects isn’t easy though! That’s why I developed the next site.

Bonus!

11. Plasis Jobs
Plasis Jobs aggregates freelance development jobs from 8 of the above 10 sites, so instead of having to check lots of different sites everyday to make sure you don’t miss out on a great new project you only need to check one! It also filters the jobs so that only freelance development jobs are shown, rather than full time jobs, or jobs for designers.

Suggestions?

Are there any freelance development sites you use that aren’t listed here? Please leave a comment!

PHP London

March 9, 2009

Ever since moving to London last August I’ve been meaning to attend some of the many technology based meetup groups. Last Thursday I finally got round to it, and attended PHP London.

Andrew Betts gave a great talk on scaling web applications, entitled “Do You Scale?”. The slides are below:

I’ll certainly be going back to PHP London next month. I’m also going to make more of an effort to attend some of the other meetups, which will hopefully be as informative and enjoyable as this one.