Archive for the 'PHP' Category

Using Jeditable for inline editing

Thursday, February 4th, 2010

Using the Jeditable plugin for jQuery enables inline editing of a block of text. This makes it easy to take a static view and simply drop in editability. Say we start with the following HTML:

<label>Name:</label> <span class="employeeName">Ben Scott</span><br/>
<label>Rating:</label> <span class="employeeRating">Highly awesome</span>

We want to be able to edit the employeeName and employeeRating spans. We need two actions (asssuming an MVC framework) to update the name and rating. The URLs might be something like /employee/set_name/{id} and /employee/set_rating/{id}. Each action should accept HTTP POST, take the new value in $_REQUEST['new_value'] or similar, and return a HTTP status of 200 on success and 500 on failure, with the error message in the response. For example, using Slab (my PHP5 MVC framework, in development) the set_name action might be like this:

class EmployeeController extends AppController {
	function set_name($id) {
		$this->Employee->save(array(
			'id' => $id',
			'name' => $this->data['new_value']);
		));
		return $this->ajaxSuccess($this->data['new_value']);
	}
}

Slab catches uncaught exceptions and returns an AJAX failure with the exception message as the body of the response.

To hook up the fields to the actions, modify the HTML to include the URLs to the actions. While this means the markup has behavioural elements and isn’t purely presentational (and has a non-standard attribute), it makes the script a bit simpler and easy to move into a static .js file, and is a quick way to get a page working.

<label>Name:</label> <span class="employeeName" editUrl="/employee/set_name/7">Ben Scott</span><br/>
<label>Rating:</label> <span class="employeeRating" editUrl="/employee/set_rating/7">Highly awesome</span>

Now for the script itself:

$(function(){
	$('.employeeName, .employeeRating').editable(
		function(value, settings) {
			return $.ajax({
				url: $(this).attr('editUrl'),
				data: { 'new_value': value },
				async: false,
				type: 'post'
			}).responseText;
		}, {
			indicator: 'Saving...',
			tooltip: 'Click to edit',
			onblur: 'submit'
		}
	);
});

The first argument is a function that returns the new value of the field. This is important when doing things like replacing line breaks with <br/> which we’re not worrying about, but it also gives us the ability to write our own AJAX code. By default Jeditable makes assumptions about how the update is done. The async option in the call to $.ajax() blocks until the call returns, and lets the function return the response of the AJAX call. The second argument are options to set some text to show while calling the update function, the tooltip to display when hovering over the span, and to submit changes on blurring the input which makes it seem a bit more usable when there are multiple fields.

New Hash Tool

Wednesday, July 29th, 2009

Latest installment in the Tiny tools I can write rather than sit on Facebook or watch Masterchef series. I’ve just made a new Hash tool. This takes an input and applies a hashing algorithm. This is useful for example when inserting a password directly into a database. Like the other two tools this one is open source via Creative Commons and source is available here. Not that it’s brain surgery, but it’s my own little code garage sale.

Updated password generator

Tuesday, July 28th, 2009

I’ve added fake GUID and hexadecimal-only options to my uber-cool password generator. Yay.

An IP address tool for teching around the internet

Saturday, July 11th, 2009

I’ve added a super simple tool that just returns the requester’s external IP, which is useful when debugging and for tech support. No biggie, there’s a heap of other tools around the place, but I can never remember the right variation.

The tool is at myip.belfryimages.com.au.

Password Generator

Wednesday, July 8th, 2009

I just made a quick tool for generating strong passwords quickly. It’s hosted at http://pwdtool.belfryimages.com.au. Now I should probably have written something to remind me where I left my keys… hmm….

CakePHP tip: setting the layout from a view

Saturday, January 24th, 2009

This works in CakePHP 1.1.19.6305 (not tested in 1.2, but I can’t see why not). I needed to set the layout used for a static view (under /pages), so there was no controller, which is where all of the documentation says to set the layout. It turns out that the View class has a layout member which works in exactly the same way, so adding $this->layout = 'empty' works as you would expect (assuming you’re expecting the ‘empty.ctp’ layout, which I was). This would work for a view under a controller, although the recommended way would be via the controller in that case.

Cookies not being sent back to the server

Saturday, January 26th, 2008

I’ve been playing with CakePHP, which is an MVC framework for PHP (the PHP equivalent of Ruby on Rails) and had this annoying bug where the session cookie (which holds a session hash) gets regenerated with every page request. This had the effect that, when I saved my User object in the session after validating the login, the cookie’s value would change to a new hash (losing the auth data), meaning that the logged in status was lost when requesting a new page.

I knew that the session was getting the user data inserted, because I could see the sessions and data being created on each request. I also knew that the browser was receiving the cookie with the last generated session hash. So, since CakePHP generates a new session for each request that isn’t accompanied by a session cookie, I figured that my browser wasn’t sending the session cookie with the request.

Point 1: if you can’t make your session stick, see if you’re getting a different session hash each request, because maybe the server just isn’t receiving the cookie.

Then I tried to figure out why cookies weren’t being sent. It turns out that having a space in the url of a website (which gets converted to %20, as in http://localhost/Internal%20Projects/CakeTest) stops cookies from being sent. This may have something to do with the web server (Apache 2.2.26) or the fact that I’m using a preconfigured WampServer Apache-MySQL-PHP stack, but it is common to both Firefox 2 and IE7. Which brings me to…

Point 2: don’t put spaces in your development server’s folder names when using cookies.