Wednesday, December 14, 2011

Project Euler and Me

Thanks to Ailin and Adam that introduce me the Project Euler and now I am totally addicted with it. After 8 years not using C as my primary programming language, is time for me to pick it up and do something with C ideology - small footprint with great performance!

Below is the question from Project Euler and the code I had written to solve it. Feel free to download them then make it better or give comment and suggestion on the solution. Programmer ROCKS!

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');
24      return false; //For testing purpose, it should be return true
25   }
26 </script>
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.