Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Friday, December 9, 2011

FORM validation with jquery.ajax

When <FORM> validation meet the JQUERY.AJAX

Below are the basic <FORM> with validation before the form submitted.

1  <script>
2    function validateform() {
3       $.ajax({
4           type: "POST",
5           url: "/country/checkcountry",
6           data: "code="+countrycode+"&name="+countryname,
7           dataType: 'json',
8           success: function (data, status)  {
9               if (data.result != 'OK') {
10                 console.log('check ok'); 
11                 return false
12              } else {  
13                 console.log('check not ok'); 
14                 return true
15              }
16          },
17          error: function (data, status) {
18              console.log('check error') 
19              return false
20          }
21      });         
22      console.log('should never show');
23   
24      return false; //For testing purpose, it should be return true
25   }
26 </script>
27
28 <FORM action="/country/addcountry" method="post" onsubmit="return validateform()">
29     ..........................
30 </FORM>


At the first glance, the java script seem logically fine. Actually, there are TWO major issue in that java script!

Wednesday, December 7, 2011

Document.getElementById return null

The problem
Document.getElementById() - The most useful and common use features while developing in javascript. Some how this function call will failed unexpected without any javascript error. The most common error for this function is returning a NULL even we can 100% confirm the element with correct ID is there!

The catch
Come on, this is not a browser issue nor javascript issue. There are few possible that trigger this issue. Listed below is what I faced before, if you have more please share by replying a comment. TQ.

1.  ID had been used more than once in a HTML page
Check is the ID the element has been used more than once in a HTML. The most easier way to debug is right click on the web browser and click VIEW SOURCE (CTRL U). From there use the find tool to search for the ID that document.getElementById() cannot found.

2. Nested <FORM> 
document.getElementById() return null while trying to get a <FORM> element? Make sure that the <FORM> element you try to get is not under another <FORM> element! This error is usually happen while using PHP framework or template based framework.

Try to move the inner <FORM> element out from the parent form. For Symfony PHP framework, sometime this is hard to archive because we are good programmer that make our code modular enough to screw ourselves up :P . In this case, you can use the SLOT concept in Symfony to solve this problem.

You know something is not listed here, please reply a comment to share. TQ.

Monday, October 10, 2011

Panes Freezer - The jquery plugin


About

When working with large table, it gets a bit frustrating, when you go to read the data in a distant row or column and the headings at the top of the page or down the side of the sheet disappear.

If you are like me, you're never quite sure if you are looking in the right spot so you scroll back and forth trying to read first the heading and then the data, heading then the data, until you're almost sure you have it right.

To avoid this back and forth waste of time, use this jquery plugin. This plugin allows you to lock specific rows and columns so that they will always be visible on screen no matter how far you scroll to the right or down.

Idea of this plugin is no existing coding modification need to be done, just with additional <DIV> and call the plugin code shown below, that's all!

$('#mydiv_1_0').panesfreezer({
    'headerrow': 1,
    'headercolumn' : 1
});


Demo


Supported browser

Below table show the supported browser for this plugin
No Browser Version OS Support
1 Google Chrome  14.0.835.202 m Windows 7 Y
2 Firefox  7.0 Windows 7 Y
3 Safari 5.1 Windows 7 Y
4 Internet Explorer 9 n/a n

Links

jquery plugin page - http://plugins.jquery.com/project/panesfreezer
Github page - https://github.com/comcrazy/Panes-Freezer/
Author blog - http://comcrazy-devcenter.blogspot.com/ @ http://comcrazy.info/

Thursday, September 8, 2011

Javascript Multi-Dimension Array to JSON Converter

The Problem
Lately ask by one of my programmer - How to pass a javascript multi-dimension array back to PHP? First thing strike in my mind, JSON.stringify! But after some quick reading and testing, found out that stringify does not support cyclic data structures.

Then brainstorming session begin. Why need to pass in multi-dimension array, why not this and why not that... And last, he just said "This is the way I want, can you make it happen or not?".

Thanks for his challenge, here by is the solution.

Saturday, January 15, 2011

IE8 : Object doesn't support this property or method

The problem
Try to load following html to IE8 then insert some text to the input box and click the button. It should just alert the text you had key in but it give a javascript error "Object doesn't support this property or method". Below code work perferly on FF 3.5.3.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>ID Test</TITLE>
<script type="text/javascript">
function testnow()
{
 inputvalue = document.getElementById('inputvalue').value;
 alert ("input value is ["+inputvalue+"]");
}
</script></HEAD>
<BODY>

<H3>Input Text</H3>
<input id="inputvalue" type="tet"></input>
<BR/>
<button onclick="testnow()">click me</button>
</BODY></HTML>

Friday, June 26, 2009

Freelance Projects

Following is some of the projects (wish can be published) I had done previously, mainly in PHP and javascript.