Archive for November, 2010

Autofocus on a textfield after page load [HTML]

November 30, 2010 Leave a comment


After page load, you want to put the focus on a textfield. For instance you have a form and you want to focus on the first field thus users can type immediately. See for instance the Google search page.


I tried several variations and this is what worked for me:

<body onload="">

Here I suppose to have the following form:

<form name="login_form" action="process.php" method="POST">
    <input type="text" name="email" />

This tip is from here.

Update (20151007)
As it is pointed out here, you can use the autofocus attribute on HTML5 compatible browsers:

<input name="myinput" value="whatever" autofocus />
Categories: html, javascript Tags:

Send HTML form upon confirmation [HTML]

November 29, 2010 Leave a comment


Before sending a form, you want a confirmation from the user. If his/her reponse is positive, the form will be sent.


<form action="process.html" method="POST">
    <input type="button" onclick="if (confirm('Are you sure you want to submit this form?')) submit();" value="Submit">

This tip was taken from here.

Categories: html Tags: , ,

Disable Enter in HTML forms

November 29, 2010 Leave a comment


You have an HTML form with several submit buttons. Each button sets some variables that you need for processing the form correctly. You want to disable sending the form with Enter, you only want to allow the buttons.


<script type="text/javascript">

function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = ( ? : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}

document.onkeypress = stopRKey;


This tip is from here.

Categories: html Tags: ,

Accept positive integers only (string to int) [PHP]

November 26, 2010 Leave a comment


You have a form and in a field you want to accept positive integers only. However, in PHP the conversion from string to int is a bit strange:

$a = "1.3";
print (int)$a;		// => 1, no error
$a = "7 sins";
print (int)$a;		// => 7, no error
$a = "0";
print (int)$a;		// => 0
$a = "hello";
print (int)$a;		// => 0 again (Did we convert "0", or is it an error? We don't know.)

There is no exception raised if the input couldn’t be converted correctly.


if (!( is_numeric($val) and ((string)(int)$val === (string)$val) and ((int)$val > 0) )) 
    // not a positive integer

Read it like this: (a) $val must be numeric, and (b) the string $val must be converted into the same integer number, and (c) $val must be positive. If it’s not true (see the negation (!)), then $val is not a positive integer.


Inspired by a comment here.

Categories: php Tags: ,

Foreign keys with phpMyAdmin [MySQL]

November 25, 2010 Leave a comment


You are using MySQL and you want to set foreign keys with phpMyAdmin.


By default, phpMyAdmin uses the storage engine MyISAM. If you want foreign keys, you must use the engine InnoDB instead. Steps:

  • choose the table you want to modify
  • choose the tab Operations
  • here, change the Storage Engine to “InnoDB” (do this with all the tables in the current database)
  • choose the tab Structure, then follow the link Relation view
  • now you can set the foreign keys and decide what to do on delete/update
Categories: mysql Tags: ,

Hacking with BackTrack Linux

November 24, 2010 1 comment

I’ve been using Ubuntu for years but I just heard about BackTrack Linux today. It’s an Ubuntu-based distribution with a collection of security and forensics tools. You can install it on your HDD, run it on a live CD, or install it on a USB drive.


Categories: ubuntu Tags: , , ,

Measure execution time in PHP

November 23, 2010 Leave a comment


You want to measure the execution time of a piece of code.

Method #1

$start = (float) array_sum(explode(' ',microtime())); 
// PHP code whose execution time you want to measure
$end = (float) array_sum(explode(' ',microtime()));
print "Processing time: ". sprintf("%.4f", ($end-$start))." seconds."; 

I found this tip here.

Method #2 (update 20110205)

There is a simpler solution; you can also write $start = microtime(true). See the PHP documentation here.

I use this method for simple profiling. For this, I use a class:

class Common
    * Get current microtime as a float. Can be used for simple profiling.
   static public function get_microtime() {
      return microtime(true);

    * Return a string with the elapsed time.
    * Order of $end and $start can be switched.
   static public function elapsed_time($end, $start) {
      return sprintf("Elapsed time: %.4f sec.", abs($end - $start));

Usage example:

$profile_start = Common::get_microtime();
// code to be profiled comes here
$profile_end = Common::get_microtime();
print Common::elapsed_time($profile_end, $profile_start);
Categories: php Tags: , , ,

Get every new post delivered to your Inbox.

Join 82 other followers