Really Easy Field validation with Prototype


Here’s a form validation script that is very easy to use.

Really Easy Field validation with Prototype

Instructions

The basic method is to attach to the form’s onsubmit event, read out all the form elements’ classes and perform validation if required. If a field fails validation, reveal field validation advice and prevent the form from submitting.

Include the javascript libraries:

You write elements like this:

passing the validation requirements in the class attribute.

You then activate validation by passing the form or form’s id attribute like this:>

new Validation(‘form-id’); // OR new Validation(document.forms[0]);

</p

It has a number of tests built-in but is extensible to include your custom validation checks.

The validator also avoids validating fields that are hidden or children of elements hidden by the CSS property display:none. This way you can give a field the class of ‘required’ but it’s only validated if it is visible on the form. The demo illustrates what I am talking about

Options

Here’s the list of classes available to add to your field elements:

  • required (not blank)
  • validate-number (a valid number)
  • validate-digits (digits only)
  • validate-alpha (letters only)
  • validate-alphanum (only letters and numbers)
  • validate-date (a valid date value)
  • validate-email (a valid email address)
  • validate-url (a valid URL)
  • validate-date-au (a date formatted as; dd/mm/yyyy)
  • validate-currency-dollar (a valid dollar value)
  • validate-selection (first option e.g. ‘Select one…’ is not selected option)
  • validate-one-required (At least one textbox/radio element must be selected in a group – see below*)

*To use the validate-one-required validator you must first add the class name to only one checkbox/radio button in the group (last one is probably best) and then place all the input elements within a parent element, for example a div element. That way the library can find all the checkboxes/radio buttons to check and place the validation advice element at the bottom of the parent element to make it appear after the group of checkboxes/radio buttons.

When the validation object is initialised you can pass the option {stopOnFirst : true} to enable the stop on first validation failure behaiour. The demo above has this set to false which is the default. If set to true only the first validation failure advice will be displayed when the form is submitted instead of all at once.

new Validation(‘form-id’,{stopOnFirst:true});

You can also pass the option {immediate : true} to enable field valiation when leaving each field. That is on the onblur event for all the form elements.

By default the library will add an event listener to the form’s onsubmit event and stop the event if the validation fails. If you pass the option {onSubmit : false} it wont do that. This way you can call the validate function manually within your own javascript.

By default the library will focus on the first field that contains an error. If you pass the option {focusOnError : false} it wont do that.

You can also pass the option {useTitles : true} to make the field validators use the form elements’ title attribute value as the error advice message.

You can set callbacks by using the options {onFormValidate : yourFunction, onElementValidate : yourFunction}.

onFormValidate is called after form validation takes place and takes two arguments: the validation result (true or false) and a reference to the form. OnElementValidate is called after each form element is validated and also takes 2 arguments: the validation result (true or false) and a reference to the form element.

Instead of using the error message in the validator you can create your own validation advice page element. Now when the script is creating the advice element it first looks for an element with an id matching ‘advice-‘ + validation-class-name + ‘-‘ + element.id and if not found then one matching ‘advice-‘ + element.id . If your form element does not have an id attribute then match the name attribute. If it finds an element it will make that one appear. See the ‘Donation’ field in the demo for an example. If you make a custom validation advice element make sure you set the style to display : none .

Also if you reference the effects.js file from Scriptaculous in your page head, it’ll use a fade-in effect for the validation advice.

CSS Hooks

As well as the validation css classes above, the validation library uses CSS classes to indicate validation status:

validation-failed and validation-passed

The validation advice element has a class of validation-advice and an id matching the following pattern

‘advice-‘ + validation-class-name + ‘-‘ + element.id

so if the field ‘ birthdate’ uses the ‘ validate-date’ validation class then the validation advice element will have an id of ‘ advice-validate-date-birthdate’.

Advertisements

Select random value from MySQL database table.


The simplest way of selecting random rows from the MySQL database is to use “ORDER BY RAND()” clause in the query.

Solution 1 [SQL QUERY]

SELECT * FROM `table` ORDER BY RAND() LIMIT 0,4;

This query select 4 random value every refresh?

The problem with this method is that it is very slow. The reason for it being so slow is that MySQL creates a temporary table with all the result rows and assigns each one of them a random sorting index. The results are then sorted and returned.

There are several workarounds to speed things up.

The basic idea is to get a random number and then select a specific row using this number.

In the case that all the rows have unique ids we will just have to pick a random number between the smallest and the biggest id and then select the row with id that equals that number. To make this method work when ids are not evenly distributed we will have to use “>=” operator instead of “=” in the last query.

To get the minimum and maximum id values in the entire table we will use MAX() and MIN() aggregate functions. These functions will return minimum and maximum value in the specified group. The group in our case is all the values of `id` column in our table.

Solution 2 [PHP]

$range_result = mysql_query( ” SELECT MAX(`id`) AS max_id , MIN(`id`) AS min_id FROM `table` “);

$range_row = mysql_fetch_object( $range_result );

$random = mt_rand( $range_row->min_id , $range_row->max_id );

$result = mysql_query( ” SELECT * FROM `table` WHERE `id` >= $random LIMIT 0,1 “);

As we mentioned this method is limited to tables with unique id for each row. What to do if it’s not the case?

The solution is to use the MySQL LIMIT clause. LIMIT accepts two arguments. The first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1).

To calculate the offset to the first row we will generate a random number between 0 and 1 using MySQL’s RAND() function. Then we will multiply this number by number of records in the table, which we will get using COUNT() function. Since LIMIT arguments must be integers and not float values we will round the resulting number using FLOOR() function. FLOOR() is an arithmetic function that calculates the largest integer value that is smaller than or equal to the expression. The resulting code will look like this:

Solution 3 [PHP]

$offset_result = mysql_query( ” SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `table` “);

$offset_row = mysql_fetch_object( $offset_result );

$offset = $offset_row->offset;

$result = mysql_query( ” SELECT * FROM `table` LIMIT $offset, 1 ” );

In MySQL 4.1 and later we can combine two previous methods using subquery like so:

Solution 4 [SQL]

SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 1;

This solution has the same weakness as the solution 2 e.g. it only works for tables with unique ids.

Remember the reason we started looked for alternative ways of selecting random rows? Speed! So how do these methods compare in terms of execution times. I am not going to go into specifics of hardware and software configuration or give precise numbers. The approximate results are:

  • The slowest method is solution 1. Let’s say that it took 100% of time to execute.
  • Solution 2 took 79%.
  • Solution 3 – 13%.
  • Solution 4 – 16%.

The winner is solution 3.

How to active curl on xampp


Your xampp installation most likely already has curl support built-in. You just have to turn it on. It’s not difficult at all.  That’s not necessary for Windows versions of PHP.

Find your php.ini file (probably in xampp\apache\bin\php.ini and open it in notepad or another plain text editor

search for the line that says:
;extension=php_curl.dll

remove the semicolon from the beginning of the line.

save the file

restart apache

check a phpinfo() program to see if you now have curl support listed.

In a standard xampp build, this is all you have to do. If you installed apache and PHP in another way, you might need to move some dlls to the windows\system directory.
(libeay32.dll and ssleay32.dll)

Best of luck to you.

Breaking referential integrity in MySql


Integrity constraint violation: 1452 Cannot add or update a child row: a foreign
key constraint fails (`sumaria_magento/mg_catalog_category_product_index`,
CONSTRAINT `FK_CATALOG_CATEGORY_PRODUCT_INDEX_PRODUCT_ENTITY` FOREIGN KEY
(`product_id`) REFERENCES `mg_catalog_product_entity` (`entity_id)

Reason..
The Constraint dont find any Parent keys in parent table. But children for these Product ids are present. That means… database have Children entries without Parent …

Is it possible to break the referential integrity when a foreign key constraint is set on an InnoDB table? The answer is an (unfortunate?) yes. It’s not a bad feature that it’s possible to temporarily turn @@foreign_key_checks off, because sometimes it’s necessary to change data in a way that referential integrity needs to be broken during the modification process. But most of the time, the data should be valid, when modification is finished. So what can be done?

A little example demonstrates this:

mysql> CREATE TABLE table_one (
->   id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
->   ) ENGINE = InnoDB;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO table_one (id) VALUES
->   (1), (2), (3);
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> CREATE TABLE table_two (
->   id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
->   id_table_one INT NOT NULL,
->   FOREIGN KEY (id_table_one) REFERENCES table_one(id))
->   ENGINE = InnoDB;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO table_two (id_table_one) VALUES
->   (3), (2), (2), (3), (1);
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM table_one; SELECT * FROM table_two;
+—-+
| id |
+—-+
|  1 |
|  2 |
|  3 |
+—-+
3 rows in set (0.00 sec)

+—-+——-+
| id | id_table_one |
+—-+——-+
|  5 |     1 |
|  2 |     2 |
|  3 |     2 |
|  1 |     3 |
|  4 |     3 |
+—-+——-+
5 rows in set (0.00 sec)

mysql> INSERT INTO table_two (id_table_one) VALUES (4);
ERROR 1452 (23000): Cannot add or update a
child row: a foreign key constraint fails
(`test/table_two`, CONSTRAINT `table_two_ibfk_1` FOREIGN KEY
(`id_table_one`) REFERENCES `table_one` (`id`))

mysql> SET @@foreign_key_checks = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO table_two (id_table_one) VALUES
->   (2), (1), (4), (2);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> SET @@foreign_key_checks = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM table_one; SELECT * FROM table_two;
+—-+
| id |
+—-+
|  1 |
|  2 |
|  3 |
+—-+
3 rows in set (0.00 sec)

+—-+——-+
| id | id_table_one |
+—-+——-+
|  5 |     1 |
|  7 |     1 |
|  2 |     2 |
|  3 |     2 |
|  6 |     2 |
|  9 |     2 |
|  1 |     3 |
|  4 |     3 |
|  8 |     4 |
+—-+——-+
9 rows in set (0.00 sec)

The record no 8 in table table_two has now an invalid value, due to the fact that we have set the foreign key verification temporarily to off (0).

How to Get the Current Page URL


Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. Another example maybe a page URL has Querystring info appended to it and you need to send an email off to someone with that same exact URL and Querystring information. There are plenty of other reasons as well. Here is how you can do that.

Add the following code to a page:

<?php
function selfURL() {
$s = empty($_SERVER[“HTTPS”]) ? ” : ($_SERVER[“HTTPS”] == “on”) ? “s” : “”;
$protocol = strleft(strtolower($_SERVER[“SERVER_PROTOCOL”]), “/”).$s;
$port = ($_SERVER[“SERVER_PORT”] == “80”) ? “” : (“:”.$_SERVER[“SERVER_PORT”]);
return $protocol.”://”.$_SERVER[‘SERVER_NAME’].$port.$_SERVER[‘REQUEST_URI’];
}

function strleft($s1, $s2) {
$values = substr($s1, 0, strpos($s1, $s2));
return  $values;
}
?>
You can now get the current page URL using the line:
<?php
print $currenturl = selfURL();

OpenposbravoPOS Build from open source file


Few days ago I have discussed Openbravo POS systen intregation with MySql. Today I will discuss How to built OpenbravoPOS after modification logo, image etc.

 

Requirements:

 

Configuration:

  • First we have to need environment variable configuration
  • Go to System properties
  • Select Advanced
  • Click Environment variables from bottom
  • First create a User variable. Variable name: JAVA_HOME and Variable value: C:\Program Files\Java\jdk1.6.0_07 its depends upon your jdk installation directory and click ok.
  • Now we go System variables part. Select variable name Path and click Edit button. Add at the end of line ;C:\Program Files\Java\jdk1.6.0_07\bin;D:\openbravopos\apache-ant-1.7.1-bin\apache-ant-1.7.1\bin
  • C:\Program Files\Java\jdk1.6.0_07\bin its depends upon your jdk installation directory.
  • D:\openbravopos\apache-ant-1.7.1-bin\apache-ant-1.7.1\bin its depends upon your Apache ant directory where you extract Apache ant .

User Variable

System Variable

 

 

Build process:

  • Go to run from start menu, type cmd and click ok button.
  • Go to your Openbravopos directory. Suppose your Openbravopos source directory is d: now type command prompt like this E:\Openbravo POS\openbravopos_2.20_src
  • Now type ant and press Enter for check Apache ant working or not. If any problem then you will get an error or pos build will be successful. If successful then go to your Openbravopos directory/ build/dist folder and unzip openbravopos_2.20_bin.zip like this zip file.
  • Open your extract folder and click start.bat file. If you want default database configuration then click ok or click cancel and configure as your wish.