Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

Sunday, January 23, 2011

Jquery Best Practices and Tips.


1.  How To Check If An Element contains a certain class or element with has():

//jQuery 1.4.* includes support for the has method.

$("input").has(".mail").addClass("mail_icon");


2. How To Detect Any Browser:

Detect Safari (if( $.browser.safari)),
Detect IE6 and over (if ($.browser.msie && $.browser.version > 6 )),
Detect IE6 and below (if ($.browser.msie && $.browser.version <= 6 )),
Detect FireFox 2 and above (if ($.browser.mozilla && $.browser.version >= '1.8' ))

3. Always start the selection  From an #id

The fastest selector in jQuery is the ID selector ($('#someid')).
This is because it maps directly to a native JavaScript method, getElementById().

Read More

Sunday, January 9, 2011

Submit Form with JQuery , PHP, HTML,MYSQL

Let us understand with a simple example of how to submit a form to a database with JQuery, PHP, HTML, MySql.

This is a direct working code with explanation given whereever required.


<div class="container">  
<form id="submit" method="post">  
<fieldset>  
              <legend>Enter Employee Information</legend>  
  
              <label for="fname">Employee First Name:</label>  
           <input id="fname" class="text" name="fname" size="20" type="text">  
  
              <label for="lname">Employee Last Name:</label>  
              <input id="lname" class="text" name="lname" size="20" type="text">  
  
            <button class="button positive"> <img src="../images/icons/somepic.png" alt=""> 
                 Add Employee </button>  
  </fieldset>  
</form>  
<div class="success" style="display:none;">Employee has been added.</div>  
</div>  


// It is relatively easy to make ajax calls with jquery. we need to pass type( GET or POST),
// URL ( of the destination), data,  success( what to be done post successful execution).

$(document).ready(function(){  
    $("form#submit").submit(function() {  
    // we want to store the values from the form input box, then send via ajax below

    var fname     = $('#fname').attr('value'); 

// We are storing it in the variable called “fname” and we are taking it from the input
       //  field with the id of “fname” and the “.attr(‘value’) tells jQuery to take the value from
       // the attribute of the value from the input field

    var lname     = $('#lname').attr('value');  
        $.ajax({  
            type: "POST",  
            url: "ajaxcalls.php",  
            data: "fname="+ fname +"& lname="+ lname,  
            success: function(){  
                $('form#submit').hide(function(){$('div.success').fadeIn();});  
  
            }  
        });  
    return false;  
    });  
});  



Add this in ajaxcalls.php File:



<?php  
  
    include ("../../inc/config.inc.php");  
  
    // Employee INFORMATION  
    $fname        = htmlspecialchars(trim($_POST['fname']));  
    $lname        = htmlspecialchars(trim($_POST['lname']));  
  
    $addClient  = "INSERT INTO Employees (fname,lname) VALUES ('$fname','$lname')";  
    mysql_query($addClient) or die(mysql_error());  
  
?>  


Hope you like it.

captcha code with PHP

This is php script used to generate the captcha message.


somcaptcha.php:
---------------


<?php
session_start();
$ranStr = md5(microtime());
$ranStr = substr($ranStr, 0, 6);
$_SESSION['cap_code'] = $ranStr;
$newImage = imagecreatefromjpeg("somecapimg.jpg");
$txtColor = imagecolorallocate($newImage, 0, 0, 0);
imagestring($newImage, 5, 5, 5, $ranStr, $txtColor);
header("Content-type: image/jpeg");
imagejpeg($newImage);
?>

Now we need to validate the generated capatcha code with information submitted in user.



<?php
session_start();
$cap = 'notEq'; // storing the value in a php variable to jquery variable to show alert for quick debugging.
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
if ($_POST['captcha'] == $_SESSION['cap_code']) 
{
// Captcha verification is Correct. PRoceed further
$cap = 'Eq';
else 
{
// Captcha verification is wrong. Take other action
$cap = '';
}
}
?>




<html>
<body>
<form action="" method="post">
<label>Name:</label><br/>
<input type="text" name="name" id="name"/>
<label>Message:</label><br/>
<textarea name="msg" id="msg"></textarea>
<label>Enter the contents of image</label>
<input type="text" name="captcha" id="captcha" />
<img src='somecaptcha.php' />
<input type="submit" value="Submit" id="submit"/>
</form>
<div class="cap_status"></div>
</body>
</html>


you can add more validations at client side .

Wednesday, January 5, 2011

Jquery Tutorial : Working with Modal Windows

Let us try to understand how to get Modal windows using Jquery.
Let us divide the code into three parts - HTML, CSS and JavaScript.
The goal of this small utility is to :
  • Able to search the whole html document for A tag NAME="modal" attribute, so when users click on it, it will display the content of DIV #ID in the HREF attribute in Modal Window.
  • A mask that will fill the whole screen.
  • Modal windows that is simple and easy to modify.

1. HTML code 


1.     <!-- #dialog is the id of a DIV defined in the code below -->  
2.     <a href="#dialog" name="modal">Simple Modal Window</a>  
3.       
4.     <div id="boxes">  
5.       
6.           
7.         <!-- #customize your modal window here -->  
8.       
9.         <div id="dialog" class="window">  
10.          <b>Testing of Modal Window</b> |   
11.            
12.          <!-- close button is defined as close class -->  
13.          <a href="#" class="close">Close it</a>  
14.    
15.      </div>  
16.    
17.        
18.      <!-- Do not remove div#mask, because you'll need it to fill the whole screen -->    
19.      <div id="mask"></div>  
20.  </div>  

2. CSS code

1.     <style>  
2.       
3.     /* Z-index of #mask must lower than #boxes .window */  
4.     #mask {  
5.       position:absolute;  
6.       z-index:9000;  
7.       background-color:#000;  
8.       display:none;  
9.     }  
10.      
11.  #boxes .window {  
12.    position:absolute;  
13.    width:440px;  
14.    height:200px;  
15.    display:none;  
16.    z-index:9999;  
17.    padding:20px;  
18.  }  
19.    
20.    
21.  /* Customize your modal window here, you can add background image too */  
22.  #boxes #dialog {  
23.    width:375px;   
24.    height:203px;  
25.  }  
26.  </style>  


--------- Refer to Java Script code in next page--------------------

subversion video