Archive for May, 2010

How to bind an event in jquery after content load dynamically

Today i face a problem while i am developing a client management system for one of my US client. The problem is that when i press button it call a php page . And the content load by jquery ajax call.
In that content there is a delete button. But the delete button event is not fire when i press button. Then i contact with one of my friend for this solution . He told me that i have to bind the method in the content after load the content. I search in my site , but didn’t able to get a better solution.
Al last after experimenting the code i am able to solve the problem. So that i think that this solution will help you to solve your problem .

View Demo:

The java script code is


 <script type="text/javascript">

  $(function()
  {
	

 $("#Search").click(function(){
	
$("#load").html(' <input type="button"  id="delete" value="Search" />');
		 
$("#delete").click(function(){

alert(this.id);
});
		  
});

 }); 
 </script>

The html code is


<div id="content">
 <input name="Search" type="submit" class="submit block" id="Search" value="Search" />
<div id="load"></div>


If you have any question then please inform us. We try to help. Hope this tutorial will help you.

How do you get the current value from a select list

Today i am developing a client management system where i fall a problem, i am not able to get the current select list values. I search on google but visiting many site i am not get good solution. At last i got the solution in one blog . So that i think to share this problem solution with you. This problem is search by many people in google by “How do you get the current value from a select list?”

To get the current value is very simple using val().

$('#selectList').val();

View Demo:

$('#selectList :selected').text()

I will create the demo and code below. This would be how to set a select multiple to an array called, “foo”.

      var foo = [];

      $('#multiple :selected').each(function(i, selected){

          foo[i] = $(selected).text();

      });

Facebook like Expanding Textbox with Jquery.

Today i will show you how to implement Facebook like expanding textbox with Jquery.Thanks to d Josh Bush for his nice watermarkinput java script file and Srinivas Tamada to developed this script.
Facebook like Expanding Textbox with Jquery.
Javascript Code
It works with Jquery latest version 1.4.2. $(‘#content’).focus(function(){} – content is the ID of the textbox. Using $(this).animate() expanding textbox height and using $(“#button_block”).slideDown() showing the update button

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function()
{

$("#content").focus(function()
{
$(this).animate({"height": "85px",}, "fast" );
$("#button_block").slideDown("fast");
return false;
});

$("#cancel").click(function()
{
$("#content").animate({"height": "30px",}, "fast" );
$("#button_block").slideUp("fast");
return false;
});

});
</script>




//HTML Code

<form method="post" action="">
<textarea  id="content"></textarea>
<div id="button_block">
<input type="submit" id="button" value=" Share "/>
<input type="submit" id='cancel' value=" cancel" />
</div>
</form>

[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-php.png" alt= "Smart Social Share" href="http://codecanyon.net/item/php-smart-social-share/202558" title="Smart Social Share (Php Plugin)" description="Smart Social share is a php plugin . Which helps you to give user to share your content with social book mark site. There are 4 plugin in this package." ref="marifdu"]

body
{
font-family:Arial, Helvetica, sans-serif;
}
#content
{
width:450px; height:30px;
border:solid 2px #006699;
font-family:Arial, Helvetica, sans-serif;
font-size:14px;

}
#button
{
background-color:#3B5998;
color:#ffffff;
font-size:13px;
font-weight:bold;
padding:4px;
}
#cancel
{
background-color:#3B5998;
color:#ffffff;
font-size:13px;

padding:4px;
margin-left:10px;
}
#content
{
font-size:14px;

}
#button_block
{
display:none;


}

Pagination with jQuery, MySQL and PHP

From many days i am thinking to make a pagination script using php and jquery. Today i am making a pagination script for you. How to implement Pagination with jQuery, PHP and MySQL. It is a simple tutorial. It’s looks big but very simple script. Take a look at this screen shot.
Pagination with jQuery, MySQL and PHP.

The tutorial contains three PHP files and two js files and one css file includes jQuery plugin.

-config.php (Database Configuration)
-pagination.php
-pagination_data.php
-jquery.js
-jquery_pagination.js
–style.css

We also include the database script in the download zip file with data so that you can run this code and check.

[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-asp.net.png" alt= "Smart Social Share" href="http://codecanyon.net/item/php-smart-social-share/202558" title="Smart Social Share (Php Plugin)" description="Smart Social share is a php plugin . Which helps you to give user to share your content with social book mark site. There are 4 plugin in this package." ref="marifdu"]

Database Table

CREATE TABLE IF NOT EXISTS `northwind_products` (
  `ProductID` int(1) NOT NULL AUTO_INCREMENT,
  `ProductName` varchar(256) NOT NULL,
  `UnitPrice` decimal(4,2) NOT NULL,
  `UnitsInStock` int(10) NOT NULL,
  PRIMARY KEY (`ProductID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=78 ;

jquery_pagination.js
Contains javascript this script works like a data controller.

$(document).ready(function()
{
//Data Loading Image
function Data_Load()
{
$("#loading").fadeIn(900,0);
$("#loading").html('<img src="bigLoader.gif" />');
}
//Hide Loading Image
function Hide_Load()
{
$("#loading").fadeOut('slow');
};

//Default Starting Page Results
$("#pagination li:first")
.css({'color' : '#FF0084'}).css({'border' : 'none'});
Data_Load();
$("#content").load("pagination_data.php?page=1", Hide_Load());

//Pagination Click
$("#pagination li").click(function(){
Data_Load();
//CSS Styles
$("#pagination li")
.css({'border' : 'solid #dddddd 1px'})
.css({'color' : '#0063DC'});

$(this)
.css({'color' : '#FF0084'})
.css({'border' : 'none'});

//Loading Data
var pageNum = this.id;
$("#content").load("pagination_data.php?page=" + pageNum, Hide_Load());
});

});

config.php
You have to change hostname, username, password and databasename.

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "learneveryday_tutorial";
$db = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die('Error connection to database server: ' . mysql_error());
mysql_select_db($mysql_database, $db)
or die('Error connection to database server: ' . mysql_error());
?>

pagination.php
User interface page.

<?php
include('config.php');
$per_page = 10;

//Calculating no of pages
$sql = "select count(*) from northwind_products";
$result = mysql_query($sql);
$count = mysql_fetch_row($result);
$pages = ceil($count[0]/$per_page);

?>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript" src="jquery_pagination.js"></script>
<link rel="stylesheet" href="style.css" type="text/css" />

<div id="loading" ></div>
<div id="content" ></div>
<ul id="pagination">
<?php
//Pagination Numbers
for($i=1; $i<=$pages; $i++)
{
echo '<li id="'.$i.'">'.$i.'</li>';
}
?>
</ul>

pagination_data.php
Simple php script display data from the messages table.

<?php
include('config.php');
$per_page = 10;
if($_GET)
{
$page=$_GET['page'];
}

$start = ($page-1)*$per_page;
$sql = "select * from northwind_products order by ProductID limit $start,$per_page";
$result = mysql_query($sql);
?>
<table width="800px">
<?php
while($row = mysql_fetch_array($result))
{
$ProductID=$row['ProductID'];
$ProductName=$row['ProductName'];
?>
<tr>
<td><?php echo $ProductID; ?></td>
<td><?php echo $ProductName; ?></td>
</tr>
<?php
}
?>
</table>

CSS Code
CSS code for page numbers.

#loading
{
width: 100%;
position: absolute;
}
li
{
list-style: none;
float: left;
margin-right: 16px;
padding:5px;
border:solid 1px #dddddd;
color:#0063DC;
}
li:hover
{
color:#FF0084;
cursor: pointer;
}

You can download the full code from bellow link.

Javascript to alternate colours for table rows

Table rows alternate colours is a easy task using java script. Alternating row color change need many place to use. So this post will help you to change alternating color change.


<html>
<head>

<script language=”JavaScript” type=”text/JavaScript”>
function alternate(id){
 if(document.getElementsByTagName){ 
   var table = document.getElementById(id);  
   var rows = table.getElementsByTagName(“tr”);  
   for(i = 1; i < rows.length; i++){          
 //manipulate rows
     if(i % 2 == 0){
       rows[i].className = “even”;
     }else{
       rows[i].className = “odd”;
     }      
   }
 }
}
</script>
</head>

<body onload=”alternate(‘thetable’)”>
<table width=”100%” id=”thetable”>

<tr><td>first colour</td></tr>

<tr><td>second colour</td></tr>

<tr><td>first colour</td></tr>

<tr><td>second colour</td></tr>

</table>

</body>

</html>