Home

Monday, April 18, 2011

How to Ajax auto refresh after 5 seconds

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script language="JavaScript">
setInterval( "Ajax();", 5000 );  ///////// 10 seconds
 
$(function() {
Ajax = function(){
 
$('#dataDisplay').prepend("Hi This is auto refresh example for you <br><br>").fadeIn("slow");
 
}
 });
</script>
<div id="dataDisplay" ></div>

Javascript E-mail Address Validation

Please input a valid email address:




Please input a valid email address:<br />
<input type="text" size=18 name="emailcheck">
<input type="submit" value="Submit" onClick="return checkbae()">

<script language="JavaScript1.2">

var testresults
function checkemail(){
var str=document.validation.emailcheck.value
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
testresults=true
else{
alert("Please input a valid email address!")
testresults=false
}
return (testresults)
}
</script>

<script>
function checkbae(){
if (document.layers||document.getElementById||document.all)
return checkemail()
else
return true
}
</script>

Email validation with PHP

<?php
   
// This function tests whether the email address is valid 
   
function isValidEmail($email){
      
$pattern "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
    
      if (
eregi($pattern$email)){
         return 
true;
      }
      else {
         return 
false;
      }  
   }
?>  
<head>
  <title>Email validation form</title>
</head>
<body>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="emailForm">
     <table>
       <tr><td>Email:<input name="email"></td><tr>
       <?php
         
if (isset($_POST['submitemail']))
         {
            if (
isValidEmail($_POST['email'])){
                echo 
"<tr><td>The email: ".$_POST['email']." is valid!</td></tr>";
            }
            else{
                echo 
"<tr><td>The email: ".$_POST['email']." is invalid!</td></tr>";
            }
         }
       
?>       <tr><td><input type="submit" name="submitemail" value="Validate email"></td></tr>
     </table>
  </form>
</body> 

Monday, April 11, 2011

How to add / remove textbox dynamically with jQuery




<html>
<head>
<title>jQuery add / remove textbox example</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<style type="text/css">
 div{
  padding:8px;
 }
</style>

</head>

<body>
<script type="text/javascript">

$(document).ready(function(){

    var counter = 2;

    $("#addButton").click(function () {

 if(counter>10){
            alert("Only 10 textboxes allow");
            return false;
 }  

 var newTextBoxDiv = $(document.createElement('div'))
      .attr("id", 'TextBoxDiv' + counter);

 newTextBoxDiv.after().html('<label>Textbox #'+ counter + ' : </label>' +
       '<input type="text" name="textbox' + counter +
       '" id="textbox' + counter + '" value="" >');

 newTextBoxDiv.appendTo("#TextBoxesGroup");


 counter++;
     });

     $("#removeButton").click(function () {
 if(counter==1){
          alert("No more textbox to remove");
          return false;
       }  

 counter--;

        $("#TextBoxDiv" + counter).remove();

     });

     $("#getButtonValue").click(function () {

 var msg = '';
 for(i=1; i<counter; i++){
      msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
 }
       alert(msg);
     });
  });
</script>
</head><body>

<div id='TextBoxesGroup'>
 <div id="TextBoxDiv1">
  <label>Textbox #1 : </label><input type='textbox' id='textbox1' >
 </div>
</div>
<input type='button' value='Add Button' id='addButton'>
<input type='button' value='Remove Button' id='removeButton'>
<input type='button' value='Get TextBox Value' id='getButtonValue'>

</body>
</html>

  DEMO :

Tuesday, April 5, 2011

what is the difference between union and union all

union is used to select distinct values from two tables 
where as union all is used to select all values including 
duplicates from the tables

Monday, April 4, 2011

Difference between include() and include_once() in php

function.php
<?php
function foo(){
echo 'some code';
}
?>
Global.php
<?php
include('FUNCTIONS.PHP');
foo();
?>
Header.php
<?php
include('FUNCTIONS.PHP');
include('GLOBALS.PHP');
foo();
?>

now if you try to open HEADER.PHP you will get an error because global.php includes function.php already. you will get an error saying that function foo() was already declared in global.php, and i also included in Header.php - which means i have included function.php two times.

so to be sure i only include function.php only ONE time, i should use the include_once() function, so my Header.php should look like this: 

Header.php
<?php
include_once('FUNCTIONS.PHP');
include('GLOBALS.PHP');
?>

now when i open Header.php, i will not get an error anymore because PHP knows to include the file function.php only ONCE  

 

Friday, April 1, 2011

Convert seconds to minutes:seconds

  String.prototype.pad = function(l, s){
    return (l -= this.length) > 0
        ? (s = new Array(Math.ceil(l / s.length) + 1).join(s)).substr(0, s.length) + this + s.substr(0, l - s.length)
        : this;
};

var seconds = 121;
document.write( Math.floor(seconds / 60) + ":" + (seconds % 60).toFixed().pad(2, "0") );
rathoddhirendra.blogspot.com-Google pagerank and Worth