Wednesday, February 24, 2016

Count and display number of characters in a textbox using Javascript & Jquery

We could do this in jQuery, assuming we want the character count displayed in a div with id="characters"


<textarea></textarea>
<span id="characters"><span>
$('textarea').keyup(updateCount);
$('textarea').keydown(updateCount);

function updateCount() {
    var cs = $(this).val().length;
    $('#characters').text(cs);
}
This is the most easiest way to do it.

Saturday, December 26, 2015

Print variable on top of the page where variable is defined bottom - php

Some time we need to print/echo a variable on the top of the page but that variable need to define after a long code bottom of the page. as we know in PHP all instructions are executed sequentially so we cannot read a variable that hasn't yet been defined. 

We can do it easily using jquery. Example

We need to print a total discount value top of the page 

<p> Total Discount : <span id="demo"></span> USD </p>

But the sum of discount value need to find after a long code 
...............................php  code bla bla bla to find a variable $sum_discount  value..................
...............................php  code bla bla bla to find a variable $sum_discount  value..................

Then asign  that value within a jquery 

 <?php    
     echo "<script type='text/javascript'> 
                        document.getElementById('demo').innerHTML = ".$sum_discount.";     
                </script>";
?>

Saturday, October 24, 2015

Skip to execute construct method when certain method called

Sometime we don't need to execute any method / code inside constructor/destructor.
example for codeigniter

class Student
{
public function __construct ()
{

        // we want to skip 2 methods(login and logout) in this 'student' class.
$exception_uris = array(
'student/login', 
'student/logout'
);

//  check called class and method from URL.If  called method are not those login/logout execute code
 
if (in_array(uri_string(), $exception_uris) == FALSE)
   {
 
       if ($this->student_m->loggedin() == FALSE)
      {
redirect('student/login');
}
}    
}
}

Total Pageviews