Feed Rss



Feb 27 2008

Javascript Image Preloader

A very simple way to load the images into the browsers cache so when they are displayed they will not have to load the file and create a delay in the browser.

// Initiate the image count variable
var imageCount = 0;
// create an array to contain the preloaded image files
var preloadImages = new Array();

// Main function to load new image into browser
function preload(imageFile)
{
imageCount++;
preloadImages[imageCount] = new Image();
preloadImages[imageCount].src = imageFile;
}

// Basic function to load the images when the browser has loaded the webpage.
window.onload = function() {
preload('images/1.jpg');
preload('images/2.jpg');
preload('images/3.jpg');
preload('images/4.jpg');
preload('images/5.jpg');
}

tag:


Feb 27 2008

Form Calculation

This is a very basic JavaScript code to calculate the value of form elements.

It can become quite useful when wanting to add items up on the fly if you prefer not to use ajax or you do not have a server side scripting language available.

tag:


Feb 27 2008

Sending a xml HTTP Request (AJAX Request)

This is a basic tutorial on how to use the xmlHTTPRequest object to send an ajax request, the way I have chosen to do this is by calling the request encased in my own object.

First we need to create an object, and it shall be called 223ajaxRequest224 and it will have three arguments theURL, sendString, callbackFunction like so.

function ajaxRequest(theURL, sendString, callbackFunction)
{
}

Now we have our object container we are going to create three methods for the object called initiateRequest, processRequest, sendGetData, sendPostData.

A quick run down of the methods:
initiateRequest: Create the request object, depending on the browser.
processRequest: Processes the output of the function, depending on the status of all active documents.
sendGetData: Sends the request using the get method.
sendPostData: Sends the request using the post method.

So let222s create the request object variable like so:

function ajaxRequest(theURL, sendString, callbackFunction)
{
var thisRequestObject;

thisRequestObject = initiateRequest();
thisRequestObject.onreadystatechange = processRequest;
}

The request object is assigned to the initiateRequest function, this function basically loads the correct object for the current browser.

function initiateRequest()
{
if (window.XMLHttpRequest)
return new XMLHttpRequest();
elseif (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
}

The onreadystatechange function is basically a function that is called everytime the ready state changes, this is so we can handle the output depending on the ready state, we will use processRequest to handle the statuses.

Now, this is what out processRequest function should look like.

function processRequest()
{
if (thisRequestObject.readyState == 4)
{
if (thisRequestObject.status == 200)
{
if (callbackFunction)
callbackFunction(thisRequestObject, sendString);
}
else
alert("There was an error: (" + thisRequestObject.status + ") " + thisRequestObject.statusText);
}

This function will basically check the ready state and when it 4 (ok) and the status is 200 (ok) then it will use the callback function we specified in the initial arguments, this is an optional function if you do not want to handle any data sent back.

You then need to create the post and get functions, both functions are identical apart from the fact that the post data specifies the Content-type of the data being posted, like so:

this.sendGetData = function()
{
if (theURL)
{
thisRequestObject.open("GET", theURL, true);
thisRequestObject.send(sendString);
}
}

this.sendPostData = function()
{
if (theURL)
{
thisRequestObject.open("POST", theURL, true);
thisRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
thisRequestObject.send(sendString);
}
}

Now we have all the functions and methods required it222s time to test the script.

Create a test html file like so, to test the sending of the data (remember to include the javascript file for the request class):

<script src="send-request.js" type="text/javascript" language="JavaScript"></script>
<script>
window.onload = function()
{
var sendData = new ajaxRequest('./php-file.php', 'request=true', showReceived);
sendData.sendPostData();
}
function showReceived(returnData)
{
alert(returnData.responseText);
}
</script>

and then create a php file, that I called 223php-file.php224 and then type anything you like in the php file.

Once you have the files in place, run the html file and then when the page loads you should have an alert box displaying the message you placed in your test file.

That222s how you send a request.

The Javascript:

<!--//

function ajaxRequest(theURL, sendString, callbackFunction)
{
var thisRequestObject;

thisRequestObject = initiateRequest();
thisRequestObject.onreadystatechange = processRequest;

function initiateRequest()
{
if (window.XMLHttpRequest)
return new XMLHttpRequest();
elseif (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
}

function processRequest()
{
if (thisRequestObject.readyState == 4)
{
if (thisRequestObject.status == 200)
{
if (callbackFunction)
callbackFunction(thisRequestObject, sendString);
}
else
alert("There was an error: (" + thisRequestObject.status + ") " + thisRequestObject.statusText);
}
}

this.sendGetData = function()
{
if (theURL)
{
thisRequestObject.open("GET", theURL, true);
thisRequestObject.send(sendString);
}
}

this.sendPostData = function()
{
if (theURL)
{
thisRequestObject.open("POST", theURL, true);
thisRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
thisRequestObject.send(sendString);
}
}
}

//-->

HTML Inset Code

window.onload = function() {
var sendData = new ajaxRequest('./php-file.php', 'request=true', showReceived);
sendData.sendPostData();
}

 

function showReceived(returnData)
{
alert(returnData.responseText);
}

Good luck

tag: ,


Feb 27 2008

Input text value change on click w/wo jQuery

This tutorial is featured on derekharvey.co.uk, the method has been applied to the username and password field.

The reason i created this javascript was because usually when you have a text field and specify a default value when you click on the field the default value needs to be removed in order to fill in your personal details however by adding the remove javascript if the user didn't log in the the value of the field would remain blank so i discovered the attribute defaultValue and this is what i have created for this pretty cool (but basic) effect.

<script>
function clearText(theField)
{
if (theField.defaultValue == theField.value)
theField.value = '';
}

function addText(theField)
{
if (theField.value == '')
theField.value = theField .defaultValue;
}
</script>

<input type="text" name="username" onblur="addText(this);" onfocus="clearText(this)" value="username" />

So let's break it down into little bits and let's start with the clearText function.

function clearText(theField)
{
if (theField.defaultValue == theField.value)
theField.value = '';
}

As you can see about the first argument in the function is 223224theField224 and this is basically the current field we apply this to. Then as you can see from the first if statement in the function we check to see if the default value is the same as the current value and if it is then we set the value to blank. This is so what when the user clicks on the field it doesn't remove the information they may have entered already.

Next we have the addText function.

function addText(theField)
{
if (theField.value == '')
theField.value = theField .defaultValue;
}

As you can see from above the principle behind the function is pretty similar to the clearText function, we have the first argument in the function as theField and all we are doing this time is checking to see if the value of the field is empty and if it is then we set the value to the default field, so this function will be applied as the user moves away from the text field.

So now we have our functions we need out text field, the text field will have two new attributes; one for onBlur and another for onFocus, as shown below

<input type="text" name="username" onblur="addText(this);" onfocus="clearText(this)" value="username" />

Well that's it for this tutorial, i hope this has helped you a little with your javascript.

Alternitavly, if you prefer to use jQuery, here is the code .....

$(document).ready(function() {
$('.rmv-dft-val').click(
function() {
if (this.value == this.defaultValue) {
this.value = '';
}
}
);
$('.rmv-dft-val').blur(
function() {
if (this.value == '') {
this.value = this.defaultValue;
}
}
);
});

Then the HTML will look like this ...

<input type="text" class="rmv-dft-val" name="username" value="username" />

The jQuery framework can be downloaded from http://jquery.com/

Enjoy ...

tag:


Feb 27 2008

HTML Select Skin

category: CSS,Projects author: lotsofcode

A more simplified version of this script is available here ...240

Utilizes:
Javascript, CSS, (x)html

Compatibility:

Firefox 1.5 / Firefox 2, MSIE7 (MSIE6 not supported)

Credits:

Howard Yeend (For suggesting the change of .value to .innerHTML so any numeric values are not displayed)

How to
In this tutorial i am going to show you how to apply a skin to the standard html select boxes, this is something i haven't seen that often enough so hopefully this should be useful for many people.

First of all we need to have an image, it should be an image of the whole select box including the border and the arrow image in the right corner, optionally you could ignore the whole default style and go your own way with this, note the dimension of the image as this will allow you to add the dimensions to the css quicker.

Lets create our selection box

<div class="skinnedSelect">
<div class="text" id="text1">Hello</div>
<select name="name" onChange="setText('text1', this);">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
</div>

Note the two div's, one containing the other div and the other containing the text placement. As usual you have the basic options available. We are also using an onChange event handler to handle the setting of the value.

The CSS is as follows, please read the comments next to the css to understand what each selectors functionality is.

<style>
<!--
/* Add the background image and set the height of the image */
div.skinnedSelect {
background: url('./images/skin.png') no-repeat scroll 0pt 100%;
height: 19px;
}
/* Set the opacity to 0 to make the options appear invisible, this allows the image to be visable */
div.skinnedSelect select {
opacity: 0;
filter: alpha(opacity = 0);
moz-opacity: 0;
width: 150px;
margin-left: -150px;
}
/* Tee text inside the box */
div.skinnedSelect .text {
float: left;
text-indent: 10px;
line-height: 19px;
width: 150px;
}
/* Shared font sizes */
div.skinnedSelect .text,
div.skinnedSelect select option
{
font-size: 11px;
color: #316D89;
}
//-->
</style>

Now as you can see from reading the comments above each selector all the individual attributes are used to style the select selection box.

The javascript is a very simple function that basically replaced the default value of the subcontainer every time you select an option.

<script>
function setText(a, b)
{
x = document.getElementById(a);
if (x)
x.innerHTML = b.options[b.selectedIndex].innerHTML;
}
</script>

I hope you like this script, if you need any assistance installing the script then please post in our forum.

Using the skin more then once.

<div class="skinnedSelect">
<div class="text" id="text1">Hello</div>
<select name="name" onChange="setText('text1', this);">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
</div>
<div class="skinnedSelect">
<div class="text" id="text2">Hello</div>
<select name="name" onChange="setText('text2', this);">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
</div>

To make things easier here is the code in full.

<style>
<!--
body {
background: #fff;
font-size: 12px;
font-family: Tahoma, Verdana, Arial;
}
div.skinnedSelect {
background: url('./images/skin.png') no-repeat scroll 0pt 100%;
height: 19px;
}
div.skinnedSelect select {
opacity: 0;
filter: alpha(opacity = 0);
moz-opacity: 0;
width: 150px;
margin-left: -150px;
}
div.skinnedSelect .text {
float: left;
text-indent: 10px;
line-height: 19px;
width: 150px;
}
div.skinnedSelect .text,
div.skinnedSelect select option
{
font-size: 11px;
color: #316D89;
}
//-->
</style>
<script>
function setText(a, b)
{
x = document.getElementById(a);
if (x)
x.innerHTML = b.options[b.selectedIndex].innerHTML;
}
</script>
<form>
<p>
<div class="skinnedSelect">
<div class="text" id="text1">One</div>
<select name="name" onChange="setText('text1', this);">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
</div>
</p>
<p>
<div class="skinnedSelect">
<div class="text" id="text2">Two</div>
<select name="name" onChange="setText('text2', this);">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
</div>
</p>
</form>

If you do not download this code then don't forget to get your self an image to use this with.

If you have any suggestions then please do not hesitate to bring them forward.

tag: , , ,


Feb 27 2008

Var Vars

category: PHP,Snippets author: lotsofcode

Var vars can be very handy, you can use them to associate a name to a variable.

<?
$my_soon_to_be_variable = 'wow_my_variable';
$$my_soon_to_be_variable = 'Now it's a VAR VAR'; // It's a var var

echo $wow_my_variable; // That's right, it's a variable
?>

tag: , ,


Feb 27 2008

Sorting Arrays

category: PHP,Tutorials author: lotsofcode

You can sort an array in many different ways using PHP using many different functions, here are a few.

sort($array);
asort($array);
ksort($array);

These three different sorting functions all can be sorted in ascending order.

The reverse functions are:

rsort($array);
arsort($array);
krsort($array);

tag: , ,


Feb 27 2008

Session based coordinates mapper

category: PHP,Projects author: lotsofcode

This is a very basic clickable image mapper based on coordinates submitted on a form. By using html form elements and php's GD Library extension. It creates a nice little box around the coordinates you click on and stores them into the session super global. This would work well if integrated with a database to store the information. Totally customizable!

tag: , ,


Feb 27 2008

PhpBB Latest Threads

category: SQL,Snippets author: lotsofcode

You can use the following code to get the latest threads from a PHPbb forum, it extracts the post information and the user information. Remember to replace "tblprefix" with your defined table prefix.

SELECT
p.post_id,
p.topic_id,
p.forum_id,
p.post_time,
p.poster_id,
pt.post_subject,
pt.post_text,
u.username
FROM
`tblprefix_posts` p
INNER JOIN
`tblprefix_posts_text` pt
ON
pt.post_id = p.post_id
LEFT JOIN
`tblprefix_users` u
ON
p.poster_id = u.user_id
ORDER BY
p.post_id DESC

tag: ,


Feb 27 2008

Make all letters caps

category: PHP,Snippets author: lotsofcode

A nice little way of making all letters into capitals is to use PHP's inbuilt function strtoupper, like so.

echo strtoupper('Hi, i am going to be all in caps in just one mo');

Will print ...

HI, I AM GOING TO BE ALL IN CAPS IN JUST ONE MO

tag: , ,