Facebook

Saturday, February 4, 2012

Looping through XML with jQuery

Handling Xml on web pages is used to be a headache when i started coding ajax events. Most of the times we will be knowing the tag name of particular data we need and we can use
xml.find('result').find('userTypeDetails')

 But this is not always the case with xml.
I have a scenario in which i get an xml like the one follows as ajax response.

<result>
    <permissionDetails>
             <addBusiness>0</addBusiness>
             <viewBusiness>1</viewBusiness>
             <viewBusinessChargeDetails>1</viewBusinessChargeDetails>
             <editBusinessProfile>0</editBusinessProfile>
             <editBusinessAccount>1</editBusinessAccount>
             <editBusinessGatewayInfo>1</editBusinessGatewayInfo>
             <deleteBusiness>1</deleteBusiness>
             <addBillingUser>0</addBillingUser>
             <viewBillingUser>0</viewBillingUser>
             <viewBillingUserChargeDetails>0</viewBillingUserChargeDetails>
             <editBillingUserProfile>1</editBillingUserProfile>
             <editBillingUserAccount>1</editBillingUserAccount>
             <deleteBillingUser>1</deleteBillingUser>
             <viewCharge>0</viewCharge>
             <editCharge>1</editCharge>
             <viewRegularReport>0</viewRegularReport>
             <addRegularReport>0</addRegularReport>
             <editRegularReport>0</editRegularReport>
             <deleteRegularReport>0</deleteRegularReport>
             <viewAdvancedReport>1</viewAdvancedReport>
             <addAdvancedReport>0</addAdvancedReport>
             <editAdvancedReport>1</editAdvancedReport>
             <deleteAdvancedReport>1</deleteAdvancedReport>
             <viewUser>0</viewUser>
             <addUser>0</addUser>
             <editUser>1</editUser>
             <deleteUser>1</deleteUser>
             <viewTemplate>0</viewTemplate>
             <addTemplate>0</addTemplate>
             <editTemplate>0</editTemplate>
             <deleteTemplate>1</deleteTemplate>
             <viewMail>1</viewMail>
             <sendMail>1</sendMail>
             <deleteMail>0</deleteMail>
             <profileDetailsView>1</profileDetailsView>
             <profileDetailsEdit>1</profileDetailsEdit>
             <viewAccount>0</viewAccount>
             <editAccount>1</editAccount>
             <deleteAccount>1</deleteAccount>
             <viewGatewayInfo>1</viewGatewayInfo>
             <editGatewayInfo>1</editGatewayInfo>
             <deleteGatewayInfo>0</deleteGatewayInfo>
    </permissionDetails>
</result>

Now i have to update an html page as in figure

One of the solution is to create checkboxes with name same as tagname in xml and iterate through the xml to check or uncheck the check box with name same as the current tag name depending upon value.
dd

xml.find('result').find('permissionDetails').each(function(){
    $(this).children().each(function(){
        var tagName=this.tagName;
        var val=$(this).text();
        if(val==1){
            $('input:checkbox[name='+tagName+']').attr('checked',true);
        }
        else if(val==0){
            $('input:checkbox[name='+tagName+']').removeAttr('checked');
        }
    })
});

Friday, February 3, 2012

code ignitor: Getting the error “Disallowed Key Characters”


Code Ignitor: Getting the error “Disallowed Key Characters” ?

   In Codeignitor, its usual for the developer getting the Error: "Disallowed Key Characters"
 This error used to drove newbies nuts. I am not sure about the reason and the fix provided by codeignitor help page not works some times[Most of the times from my experience]. It is also difficult to find what caused the error.

Here is solution that help in diagnosis of the problem.

In the core CI code in system/libraries is a file called input.php This small modification would show the actual data that caused the Problem.

Around line number 199


function _clean_input_keys($str)
{
if ( ! preg_match(“/^[a-z0-9:_\/-]+$/i”, $str))
{
exit(‘Disallowed Key Characters: ‘.$str);
// The variable $str was added to display the data in question.
}
return $str;
}

jQuery: exclude $(this) from selector


I have a set of text boxes and i want to check for duplication of set values in any of the text box. ie, no text box should contain a value that is entered in any other.

HTML
<form method="post" action="" onsubmit="return validate()">
   <table>
       <tr>
               <td>
                         <input type="textkbox" name="priority[]" />
               </td>
       <tr>

       <tr>
               <td>
                         <input type="textkbox" name="priority[]" />
               </td>
       <tr>

       <tr>
               <td>
                         <input type="textkbox" name="priority[]" />
               </td>
       <tr>

       <tr>
               <td>
                         <input type="textkbox" name="priority[]" />
               </td>
       <tr>

       <tr>
               <td>
                         <input type="textkbox" name="priority[]" />
               </td>
       <tr>
   </table>
</form>
DEMO

JAVASCRIPT


var firstError='';
function validate(){
     $('input[type="text"]').each(function(){
var mastrPriority=$(this);
$('input[type="text"]').not(this).each(function(){
var priority=$(this);
if(mastrPriority.val()==priority.val()){
  seterrormessage(mastrPriority,'Duplicate Priority Values.');
seterrormessage(priority,'Duplicate Priority Values.');
if(firstError==''){
alert("Priority Field should contain unique values.");
firstError=mastrPriority;
}
}
});

})
if(firstError!=''){
  firstError.focus();
return false;
}
else{
  return true;
}
}


function seterrormessage(field,msg){
var errorBox=$(field).parent().find('div.validationError').first();
if(errorBox.length>0){
  errorBox.text(msg);
}
else{
  $(field).after('</br><div class="validationError">'+msg+'</a>');
}
}
The function validate() iterates through each text box and compare values of each box with all others except current one. The current text box is excluded from the set of text boxes using .not(this).