All posts by Arvind Kumar

A note on DOM elements’ changed scope when using thickbox with jQuery

A similar situation may arise (atleast for me it did) when you try to invoke jQuery’s “thickbox” in conjunction with a custom onclick firing. I would explain it with the help of an example:

I have a div element (with id “myBlockTOShow”, i would say it “#myBlockTOShow” in this post further) which i display/present as a “thickbox” when a href tag/link is clicked. Here is how the div element looks like: Continue reading

How to prevent parent’s onclick event from firing when a child tag is clicked with jquery?

Generally, Javascript events bubble (listen) to the highest point in the DOM to which a click event had been attached. So even if you don’t have any other explicitly click-able elements in the div, every child element of the div would bubble their click event up the DOM to until the DIV’s click event handler catches it. Continue reading

Setting Style/CSS of select options using jQuery

This is how i set style (color here) of all of the options of a select list using jQuery.

In my case i had select options like this:

<select id=”select_list_id”>
<option value=”#595959″>Grey</option>
<option value=”#000000″>Black</option>
<option value=”#036″>Blue</option>
<option value=”#363″>Green</option>
<option value=”#632423″>Red</option>
<option value=”#403152″>Purple</option>
<option value=”#E36C0A”>Orange</option>
</select>

Follwoing the jQuery code i used to set color of options above:

  $(document).ready(function(){
	$('#select_list_id option', this).each(	
		function ()	{
			$(this).css({'color':$(this).val()});
		}
	)
  });

Setting execution time and memory limits in php

When executing large amount of data through php scripts it is common facing errors like “Fatal error: Maximum execution time of 300 seconds exceeded..”. Also, sometimes uploading large files through php scripts you might face errors like “The uploaded file exceeds the upload_max_filesize directive in php.ini..” etc. etc.. Continue reading

Simple captcha component for CakePHP 1.x

Updated on – April 11, 2013

Download Captcha Component for CakePHP 1.x

Download Captcha Component for Cakephp 2.x

Model validation to validate captcha field value included with examples. Functions(rules) required for model validation are also written in the model file.
———————————————————-

Follow these simple steps to make it working.

Copy attached font file (monofont) to ‘webroot’ folder.

Copy component file (captcha.php) to app/controllers/components

Create a function similar to the following in your controller. I have used “signups” controller for demonstration purpose. You should change controller name accordingly.

function captcha()	{
		$this->autoRender = false;
		$this->layout='ajax';
		if(!isset($this->Captcha))	{ //if Component was not loaded throug $components array()
			App::import('Component','Captcha'); //load it
			$this->Captcha = new CaptchaComponent(); //make instance
			$this->Captcha->startup($this); //and do some manual calling
		}
		//$width = isset($_GET['width']) ? $_GET['width'] : '120';
		//$height = isset($_GET['height']) ? $_GET['height'] : '40';
		//$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';
		//$this->Captcha->create($width, $height, $characters); //options, default are 120, 40, 6.
		$this->Captcha->create();
	}

Call the captcha action from within your img tag in the view file as below:

echo $form->create("Signups");
echo $html->image($html->url(array('controller'=>'signups', 'action'=>'captcha'), true),array('style'=>'','vspace'=>2)); ?>
echo 'Enter the given security code:';
echo $form->input('Signup.captcha',array('autocomplete'=>'off','label'=>false,'class'=>'','error'=>__('Failed validating code',true)));
echo $form->submit(__(' Submit ',true));
echo $form->end();

And you are done. The captcha image should look like Cakephp Captcha COmponent

Also, you can re-generate the captcha by adding (replacing code in line 2 of the above) ajax code something similar to the following:

<div id="captchaID" ><?php
echo $html->image($html->url(array('controller'=>'signups', 'action'=>'captcha'), true),array('style'=>'','vspace'=>2)); ?></div>
<?php echo $ajax->link('Can not read this code? Reload.','regenerate code',array('url'=>'reload_captcha','update'=>'captchaID')); ?>

In the code above it calls a reload_captcha function of the current controller. You can place reload_captcha in current controller or preferrably in the app_controller to make it available universally. Here’s the code:

function reload_captcha()	{
		App::import('Component','Captcha'); //load it
		$this->Captcha = new CaptchaComponent(); //make instance
		$this->Captcha->startup($this); //and do some manually calling
		$this->layout='ajax';
		Configure::write('debug',0);
		$this->viewPath = 'elements'.DS;
		$this->render('reload_captcha');
	}

It renders a reload_captcha element. Create a reload_captcha.ctp file and place it in views/elements with the following code:

<?php echo $html->image($html->url(array('controller'=>'signups', 'action'=>'captcha'), true),array('style'=>'','vspace'=>2)); ?>

There is model validation code included in the files with example of how to validate it. Files include a model with new validation entry along with two rules (functions).

Download Captcha Component for Cakephp 1.x
Download Captcha Component for Cakephp 2.x

Resume download of PART extension file in Firefox

In some cases, when a large file had been downloaded through Mozilla Firefox, downloading of file gets interrupted for some reason. The Firefox would tag the download as “failed” and you are left with nothing but a .PART file. You right click it, it would ask you to ‘retry’ which would restart the download from the beginning. In my case i had been downloading a 100MB zip file and it failed once 97MB of it was downloaded. Continue reading