Archive

Archive for June, 2011

A very nice jQuery plugin for text input hint / helper text…

June 24th, 2011 No comments

While looking for a jQuery plugin that would let me put some default text in a field and then remove it when a user focuses and before submit is clicked, I stumbled across this plugin:  labelOver

You can view an example here.

The advantage over this plugin is two fold.  First, it doesn’t actually ever put text into the input box, rather it positions the inputs Label tag over the field…  This is good for several reasons.

  • The label tag is used by screen readers, etc…
  • It works fine if javascript and css are off (not a big deal but still a good thing).
  • It lets you style the label with other html tags (like strong, etc…)
Categories: html5, jquery Tags:

Turn off jQuery .load caching…

June 22nd, 2011 No comments

IE7 has this nasty habbit of caching ajax requests very very agressivly. Which stinks because more often than not ajax content is very dynamic and thus what you don’t want cached!

Fortunatly jQuery provides an easy way to disable all ajax caching with just one statement.

$.ajaxSetup ({
    // Disable caching of AJAX responses
    cache: false
});

See: http://stackoverflow.com/questions/168963/stop-jquery-load-response-from-being-cached for the original post.

Categories: ajax, jquery Tags:

Testing websites on IE 7

June 22nd, 2011 No comments

Today I was informed that a webapp I had written had some IE7 problems… argh… I thought to myself, not ie7… I went in search of ways to test the site on ie7. I tried Microsofts Virtual PC Image of Windows Vista with IE7, but it crashed on me repeatedly. I tried loading it in VirtualBox instead of VirtualPC, but to no avail. Then I found a tool I had used at a previous job but had forgotten the name of.

IE Collection!

IE collection is actively maintained (as of this writing atleast) and can install everything from IE 1.0.

In the end, I just used ie9 developer tools to enable IE 7 browser and document modes. But, IE Collection is still a fantastic tool.

Categories: html Tags:

MVC in simple terms…

June 20th, 2011 No comments

Recently a friend of mine asked me what MVC was.  I know it took me several years to understand mvc…  So, I sent him this email which I feel pretty well explains it…

Warning…  What follows is completly my un-educated experience…  So, other’s may disagree, but I’m pretty sure this is atleast 80% accurate…

As far as classes, files, etc…  That really isn’t important.  That is more of an implementation detail.  The real key, is the concept of separating the model, the view, and the controller, whether that is done in a scripting language that doesn’t support classes in seperate files, or seperate classes in one file, whatever, while it is important to seperate them somehow, the HOW isn’t as important as the WHY, and WHAT.

In abstract terms…  The model is really where the application is.  The controller is the glue between the request, the model, and the next view, and the view is just the output of the model.

i.e.  in a perfect MVC application, you could write a command line version of the application by writing a new “controller” that takes it’s input from the command line, and new “views” that return output to the command line.  And all the business logic would live in the model.  The final version of the app was DEFINITLY NOT perfect MVC, however, you can see how at the top of the file we do all the “controller” stuff.  The Model is really mixed in with the controller stuff, although the detailed db stuff is abstracted out by Infusionsoft.  The view however is pretty pure.  Not much logic in the view.

Now, this is NOT to say there should never be logic in the view, but there shouldn’t be “business logic”.  You can have logic in the view to display select boxes, or to check a flag to decide how to display something, but the view should do what it is instructed and be pretty dumb, and really not have a clue about the internals of the model…

So, another way to look at MVC is a car…  The steeringwheel, brakes, and turn signals are the controller (the human interface INTO the car, although this is a bad example, because in a webapp, the physical manifestation of the steering wheel is actually in the view, but the hidden parts of the steering wheel that connect it to the mechanism that turns the weels is the controller, and the thing between the connection with the steering wheel and the wheels is the model).  The speedometer and dash are the view, and the engine / driveline is the model…  The speedometer doesn’t know ANYTHING about the car, all it does is display the signal that comes into it.

Another example is an elevator…  The control panel is the link between the buttons and the elevator computer, the model is all the elevator mechanisms…  And the view is both the light up buttons on the control panel, and the floor indicators most elevators have.

It’s a little difficult to use real world examples because the physical manifestation of the controller is really part of the view, whereas with webapps, the controller is the thing that takes the request and controls the model.  And the view is used to send the right information to the controller, as well as return output to the user.

 

Categories: concepts Tags:

Escaping Strings for use in Javascript from PHP

June 20th, 2011 No comments

I ran into a problem today on an application where building some inline javascript (which btw I NEVER recommend, I inherited this from someone else…   Write javascript that takes an object as a param and use json_encode instead…)…  And some data in our database had carriage returns…  Which was breaking our javascript.  I wanted to escape the string before putting it in the javascript but I didn’t want to write my own function.  After doing some searching, I discovered that json_encode will return a valid javascript string if you pass it a string instead of an array!!!

Note:  json_encode will put double quotes on the end of your string, so to fix this, you need to strip them… Use preg_replace:


function javascript_encode($toEncode){
  return preg_replace("/^\"(.*)\"$/", "$1", json_encode($toEncode));
}

Cheers!

Joey

Categories: javascript, php Tags:

Printing From Javascript

June 17th, 2011 No comments

This one is easy…


window.print();

Categories: javascript Tags:

How do I download an excel file from php?

June 16th, 2011 No comments

I must do this every 6 months and ALWAYS have to research it…
Instead of just setting the content-type as an octet-stream like usual, specify the mime type for Excel, and the browser will open it in the right program.  This isn’t JUST Ms Office, this content-type will tell the browser to open it in Open Office as well.

header(“Content-type: application/vnd.ms-excel “);
header(“Content-Disposition: attachment; filename=\”” . preg_replace(“/[^a-zA-Z0-9\s\-]/”, “”, $event_key) . “.csv\””);

Categories: php Tags:

Properly Preparing A String for a csv file in php

June 16th, 2011 No comments

The magic thing to do is to escape double quotes by making them double double quotes…  And enclosing the string in quotes.  Pretty easy…

 echo ‘”‘ . str_replace(“\””, “\”\””, $data) . ‘”, ‘;

Categories: php Tags:

Page refresh with JavaScript

June 16th, 2011 No comments

The easiest way is to do a: window.location.reload();

It is important though that you use the “POST/Redirect/Get” pattern on your pages, otherwise a window.location.reload() will cause the browser to resend any post data it sent on the last request. 

Categories: code snippets, javascript Tags: