JavaScript Logo

Using JavaScript To Run Form Events

13th March 2008 - 3 minutes read time

Creating a form as part of an AJAX control is a common practice, but when you include a submit button within the form the default behaviour of the form is to redirect to the destination supplied in action. To run a form using JavaScript you need to include two things.

<form action="javascript:return false;" method="post" id="analysisForm" >

In order to overwrite the normal operation of a form you will need to replace the action attribute with some JavaScript code that returns false. This is can be done in the form of come in-line JavaScript code (commonly called a bookmarklet) in the place of any URL in the action attribute of the form.

Next, in order to actually get JavaScript to intercept the form submission action an onsubmit event is added to the form tag.

Read the full article

PHP Logo

PHP Page Redirection

12th March 2008 - 3 minutes read time

To redirect to a different page using PHP you can use the header() function with the parameter 'Location: ' and the destination of the redirect.


However, if any headers are sent before this function call then the script will fail. To get around this you can either ensure that nothing is printed out on the page, or if that is not possible for some reason then you can use a JavaScript redirect. The headers_sent() function will allow you to see if any headers have been sent to the browser yet, if they have then you will need to use the JavaScript redirect like this:

Read the full article

PHP Logo

PHP Function To Work Out Average Values In Array

11th March 2008 - 3 minutes read time

Working out the average of a bunch of values is quite a common task, but rather than looping through the array, adding together values as you go and the using the count() function to find out the average at the end.

function average($array)
 $total = 0;
 foreach ($array as $item) {
  $total += $item;
 return $total/count($array);	

However, a much simpler way of doing things is just to use the PHP function array_sum(), which adds up all of the values in the array. Because this is done by the PHP engine it should take less time than using a for loop.

function average($array) {
 return array_sum($array) / count($array);

Tests show that the second function is only just faster when using short (2 or 3 items) arrays, but the second function is significantly faster when looking at longer (10 or more) arrays.

Read the full article

PHP Logo

Biased Random Value From Array With PHP

10th March 2008 - 2 minutes read time

Sometimes you will want to get a random value form an array in a biased random way, that is, you will want certain values to be returned more than others. Here is a function that will generate a single key from an array, with a greater change of a higher value being retrieved.

function biasedRandom($numbers){
 $total = 0;
 foreach($numbers as $number=>$weight){
  $total += $weight;
  $distribution[$number] = $total;
 $rand = mt_rand(0,$total-1);
 foreach($distribution as $number=>$weights){
   return $number;

This function is used in the following way.

Read the full article

PHP Logo

Discover Auto Increment ID After MySQL Insert With PHP

9th March 2008 - 2 minutes read time

Inserting a value into a database with an auto incrementing field is quite common. Once you insert the new row you would expect that you need to do another query to get the newly created ID.

Another option is to use the mysql_insert_id() function to retrieve the ID created by the last insert statement.

// insert
$sql = 'INSERT INTO table(colum1, colum2) VALUES(1, 2);';
// get new id
$id = mysql_insert_id();

The $id variable will now contain your most recently inserted ID.

Read the full article

PHP Logo

PHP Password Generator

8th March 2008 - 2 minutes read time

Here is a very simple function that will generate a string of random characters, ideal if you want to create a password for a new user.

function generatePassword($length=10)
 $pass = '';
 $parts = array_merge(range(0, 9),range('a', 'z'),range('A', 'Z'));	
 for ($i=0; strlen($pass) <= $length; $i++) {
  $pass .= $parts[array_rand($parts)];
 return $pass;

Use the function like this.

echo generatePassword();

To create a longer password string just pass a parameter with the function.

echo generatePassword(5);


Read the full article

WordPress Logo

Changing Your Wordpress Database Prefix

7th March 2008 - 5 minutes read time

A good security tip when installing your Wordpress blog is to change the database table prefix, the idea is that this will hide the tables from any hackers looking to compromise your blog. This can be done in the wp-config.php file and the variable $table_prefix. Changing this value from the default wp_ to, for example, blog_ will change the table wp_posts to blog_posts, making it more difficult for hackers to find it. Using blog_ is only an example, you should treat the prefix like a password, include letters and numbers to make it more difficult to find.

But what happens if you have installed your system and want to change the prefix? Well you need to change the $table_prefix variable in your wp-config.php file and alter the tables in your Wordpress database. Which one you do first is up to you, but you should go them both as fast as you can. Here are some MySQL statements that should help you to do this.

Read the full article

PHP Logo

Extract Links From A HTML File With PHP

6th March 2008 - 2 minutes read time

Use the following function to extract all of the links from a HTML string.

function linkExtractor($html)
 $linkArray = array();
 if(preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/a>/i', $html, $matches, PREG_SET_ORDER)){
  foreach ($matches as $match) {
   array_push($linkArray, array($match[1], $match[2]));
 return $linkArray;

To use it just read a web page or file into a string, and pass that string to the function. The following example reads a web page using the PHP CURL functions and then passes the result into the function to retrieve the links.

Read the full article

Apache HTTP Logo

Using mod_rewrite On Form Parameters

5th March 2008 - 6 minutes read time

Using mod_rewrite on websites is fairly straightforward and can create some lovely looking URL structures. Instead of having a URL that contains lots of odd looking parameters like this:

You can use a .htaccess file to rewrite the URL on the server side in order to shorten this to something like this:

In this occasion the value of parameter2 will always be value2 so we can just include that in the rewrite rule, which would look something like the following. $1 is a back-reference to the first parenthesized value matched in the RewriteRule.

Read the full article

PHP Logo

Simple PHP Script To Hide An Email Address In An Image

4th March 2008 - 3 minutes read time

Spam is a problem. You want to allow people who genuinely want to get in touch to see your email address, but doing this invariably leads to you getting thousands of spam emails.

One solution is to hide your email address in an image, but it can be a pain to create an image for every email address you need. A better solution is to use the PHP GD functions to create an image at runtime so that your email address is displayed, but is completely unreadable to spammers.

To to this you will need to create an image tag on your website, here is an example.

Read the full article