Search

Sunday, November 20, 2011

div scroll with jquery

Scrollable content area is looking more beautiful when we add some jquery events on it, beside this it will possible throught javascript as well, to make two functions that will call on the mouseover and the mouseout html attributes. But in this example we make it more easy with the help of .hover() event represented by jquery. You will found more about the .hover() event on the jquery official website.


#container{
 width: 500px;
 height: 200px;
 overflow: hidden;
 margin: 0 auto;
 padding: 3px;
 border: 2px solid #42826C;
 background: #A5C77F;
}
p{
 font: 13px/18px Verdana, Arial, Helvetica, sans-serif;
 padding-right:20px;
}

Here is the html part:

        

Enter the text here as much as you want.


Now here is the jquery file:

$(function(){
 
 $('#container').hover(
  function(){
   $(this).css({'overflow':'auto'});
  },
  function(){
   $(this).css({'overflow':'hidden'});
  });
});

Sunday, November 6, 2011

submit form without page refreshing using jquery, ajax and php

Ajax request is the most powerful feature of jquery, it helps to send data over the cross domain in the backend. In this post i give the very basic idea to submit a form with page refreshing and get the result. In this script i use some basic jquery and php. If you will apply this script in your own page then, I request you to validate all the data. Lets beautify the form with some CSS codes:

label{
 font: 13px/18px Verdana, Arial, Helvetica, sans-serif;
 color: #666666;
}
.inpbox{
 border: 1px solid #999999;
 padding: 2px;
 height:20px;
}
.txtbox{
 border: 1px solid #999999;
 padding: 2px;
 resize: none;
}
.buttn{
 border: 1px solid #999999;
 font: bold 13px/18px Verdana, Arial, Helvetica, sans-serif;
 color: #666666;
 padding: 3px 10px;
}

Here is the basic form layout:

 

I use table to prevent the browser confliction bug. Now lets start with some jquery stuff, before applying this stuff please insert the jquery source file from jquery official site or form ajax google api.

$('input[name="send"]').click(function(){
  var query = $('#myform').serialize();
  
  $.ajax({
   url: 'ajaxForm.php',
   data: query,
   success: function(data){
      if(data == 'send'){
       $('.result').html('Data Send Successfully!');
       
       $('input[name="name"]').val('');
       $('input[name="email"]').val('');
       $('input[name="subject"]').val('');
       $('textarea[name="query"]').val('');
      }
      else{
       $('.result').html('Error to Send Data!');
      }
     }
  });
 });

Now as you see in the above jquery codes that i request the a ajaxForm.php page through ajax request which handles all the data and return the result.

 $name = (isset($_REQUEST['name'])) ? $_REQUEST['name'] : NULL;
 $email = (isset($_REQUEST['email'])) ? $_REQUEST['email'] : NULL;
 $subject = (isset($_REQUEST['subject'])) ? $_REQUEST['subject'] : NULL;
 $query = (isset($_REQUEST['query'])) ? $_REQUEST['query'] : NULL;
 
 if( !empty($name) && !empty($email) && !empty($subject) && !empty($query) ){
  echo 'send';
 }

Wednesday, September 28, 2011

Beautify table with jquery selectors :even and :odd

If we talk about the Data Represent into the Website than it is better the best way to display your data into tables, cause IE6 creates lots of problem to in CSS to align the data. And Jquery plays his important roll to beautify that tables Data. The :even, :odd selectors make it beautiful to represent the data. Here is an example :
$('table').css({
	'border-collapse':'collapse'
});

$('tr th').css({'background':'#666666', 'color':'#ffffff', 'font-family':'verdana', 'font-weight':'normal'});

// Here using jquery selectors
$('tr:even').css({'background':'#c3c3c3'});
$('tr:odd').css({'background':'#e3e3e3'});
This code will output similar as like
Jquery Selectors Effects on Table

Friday, September 2, 2011

Get URL, URL part, URL parameters using javascript

Getting URL or URL parts through javascript is so easy. We can get protocal using this code :
	var proto    = window.location.protocol;
	alert( proto );

this code return "http:" protocol.
If you want to get host name from the URL using javascript then you can simple write this code:
	var hostName = window.location.host;
	alert( hostName );


this code return the host name like if you are running this code in local host then it returns "localhost"
if you want to get path of the URL with javascript then use this:
	var pathName = window.location.pathname;
	alert( pathName );


this code returns the path name of the current running script like you are running the script in test.html the it return the full path name with filename test.html
Or if you want to get parameter used in URL with javascript then simply do this:
	var params   = window.location.search;
	alert( params );


it returns the parameter used in the URL

now lets join all the code :
	var proto    = window.location.protocol;
	var hostName = window.location.host;
	var pathName = window.location.pathname;
	var params   = window.location.search;

	alert( proto + hostName + pathName + params );


Tuesday, May 31, 2011

dynamic select box with jquery, php, mysql

hello friends last Saturday i was working on a Real State project and i need to build a dynamic form to submit the properties in particular state and cities. I made that dynamic form with the help of php and jquery. Here is i describe the codes one by one.
First of all we need to create a Database. And after that Create Some tables related to your project. I Create a Database name 'test' in phpmyadmin. My these codes are related to this dynamic form you can change the Database name and fields as per your requirements. After create the database create a table, in this example i create a table name 'tbl_class' with these codes :

CREATE TABLE `test`.`tbl_class` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 200 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;

then insert some data with the help of these commands :

INSERT INTO `test`.`tbl_class` (
`id` ,
`name`
)
VALUES (
NULL , '10th Class'
), (
NULL , '11th Class'
), (
NULL , '12th Class'
);

with the help of same steps create one more table named 'tbl_student' and also use same insert command to insert the data in 'tbl_student' table.

now it is the time to connect the database with the help of php.
here is an example to connect the database named 'connection.php' .

#make Connection
 $hostname = "localhost";
 $username = "root";
 $password = "";
 $dbname = "test";
 
 //Connection layer
 $conn = mysql_connect($hostname, $username, $password);
 
 //Check connection and select DATABASE
 if( $conn )
  mysql_select_db($dbname, $conn);
 else
  die("Error in Connection!");
  

now create one more file 'index.php' and create some html stuff.
for example :

Dynamic Form



include the 'connection.php' file on the top of the 'index.php', like:

include 'connection.php';

then insert the data in the select option

#insert class data from database
         $query = "select * from tbl_class";
         $result = mysql_query($query) or die("Error in Query!
".mysql_error());
         
         while($row = mysql_fetch_array( $result ))
         {
          echo "";
         }

now it is the time to use jquery for the ajax request. Create a function with jquery to make simple the ajax request.
here is the code for the ajax request function:


create a 'getStrength.php' file to get the data through ajax request.

if(isset($_REQUEST['class']))
 {
  $classID = (int)$_REQUEST['class'];
  
  $query = "select * from tbl_student where classID = '$classID'";
  $result = mysql_query( $query ) or die("Error in Strength Query!");
  
  if($classID == 0)
   echo '';
  else{
   while($row = mysql_fetch_array($result)){
   echo "";
   }
  }  
 }

the above code is for the file 'getStrength.php'. The ajax request send the data to this file and get the result through this file.
You can beautify this dynamic form with some css effects.

hopefully you will enjoy this post.

Wednesday, March 16, 2011

email validation using php

Form validation is must to prevent spam and form attacks. Email validation is also must to making a form. We use Regular Expressions for validation in PHP, which is secure and highly usable on web. preg_match() and ereg() functions are using for validation. But sometimes ereg() function produce some Deprecated Error. To prevent Deprecated Error we use preg_match for email validation. Here are some codes for Email Validation :

function email_Validate($str)
 {
   if(preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $str)) 
   {
  return true;
   }
 }
 

and now we use this email validation function in our form

if(!email_Validate($email))
 {
  echo "Email not valid!"; 
 }

Beside PHP you can validate your email with javascript/jquery. Its quite easy to implement and use with the help of javascript/jquery. Here is my code for jquery and you can also implement these with javascript :

var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

//userMail is a input box name 
var email = $('#userMail').val();

if(reg.test(email) == false)
{
   alert('Email Not Valid');
}

this validation help you to prevent spamming in login system or registration form.

Related Posts Plugin for WordPress, Blogger...