Thursday, May 14, 2009

Create your own Konami code or custom code.

I have used Paul Irish code for my example.



<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>

jQuery(function($){

var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
$(document).keydown(function(e)
{
kkeys.push( e.keyCode );
if( kkeys.toString().indexOf( konami ) >= 0 )
{
$(document).unbind('keydown',arguments.callee);
//Any message or event you want goes here
alert('The Konami Code Is Activated');
}
});
});
</script>



By replacing unicode char values in this line you can define any key combination you like.
Example
var kkeys = [], konami = "38,40,75";
this means up,down,k

You can find any key code from this site http://www.javascriptkit.com/jsref/eventkeyboardmouse.shtml

Tuesday, May 12, 2009

Use Gmail to send mails in php

You can use Gmail SMTP to send emails remotely even in your localhost machine with WAMP.
First you must download phpmailer
After you extratced all zip files goto examples folder. In there you will find test_smtp_gmail_basic.php file. Just edit the code with your gmail username and password and you are ready to go.

Before executing the script goto php.ini file and remove the ; that is infront of extension=php_openssl.dll

Then restart your server.

Leave a comment if you need any help.

Monday, May 11, 2009

check only for numbers in JavaScript

This code will show you how to check only for numbers in JavaScript.


<script type="text/javascript">

var input = '112334';
var vali_regex = /^[0-9]+$/;

if(input.match(vali_regex))
{
alert('This is a number');
}
else
{
alert('Not a number');
}
</script>

Monday, May 4, 2009

How to search an array in php

You can use array_search() function to easily search values in an php array. If you didn't know about this function you should have probarbly used a foreach or for loop to find a matching value in your array.

array_search() will return the array key if found the given value. This function is case sensitive to strings.


<?php

$my_array = array('1', 'kuppiya', 'Tom', 'Hello');

$key = array_search('Tom', $my_array);

if($key === false)
{
echo 'Not found';
}
else
{
echo $my_array[$key];
}

?>

Thursday, April 30, 2009

Alternative to classic LightBox - Sexy LightBox 2.1

Sexy LightBox 2.1 is a lightweight and elegant replacement for classic Lightboxes you use. Comes with a MIT license and very easy to integrate.

Sexy LightBox

http://www.coders.me/ejemplos/sexy-lightbox-2/

Tuesday, April 28, 2009

How to check upload file extension (JavaScript)

Before uploading a file it's a good idea to check for valid file extensions you want.
Even though you can easily bypass JavaScript validation by disabling JavaScript in the browser, you can prevent genuine user errors by implementing this code.


<html>
<head>
<title>Upload Check</title>
<script type="text/javascript">

function vali_type()
{
var id_value = document.getElementById('up').value;

if(id_value != '')
{
var valid_extensions = /(.jpg|.jpeg|.gif)$/i;
if(valid_extensions.test(id_value))
{
alert('OK');
}
else
{
alert('Invalid File')
}
}
}


</script>
</head>

<body>

<input type="file" name="up" id="up"
onBlur="vali_type()" />

</body>
</html>



var valid_extensions = /(.jpg|.jpeg|.gif)$/i;
Check for .jpg, .jpeg and .gif file extentions in the end of the string
| - OR
i - Case Insensitive
$ - END

Monday, April 27, 2009

mysql_real_escape_string() is not working properly

Is the function mysql_real_escape_string() return a empty value for you? or does it not work properly as you expected.
The most common mistake is the use of the mysql_real_escape_string() before a database connection.
Make sure you use the function after a successful database connection.

If E_WARNING error level is enabled in your error_reporting you will get a E_WARNING for this mistake. It's always a good idea to make error_reporting E_ALL in the development cycle.

http://www.php.net/mysql_real_escape_string

Read the Notes section for more details.

Saturday, April 11, 2009

List of Browser User Agents to use with php curl

Here is a simple list of user agents that can be used with php curl or JavaScript

(In windows)

IE 7 - Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SU 3.21; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)

Chrome - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.53 Safari/525.19

Firefox 3.0 Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8

Firefox 2.0 - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6

Safari - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.28 (KHTML, like Gecko) Version/3.2.2 Safari/525.28.1

Opera - Opera/10.00 (Windows NT 5.1; U; en) Presto/2.2.0