Sunday 12 August 2012

jQuery Loading Animation with PHP

Here i will show you simple loading animation using jQuery. You can make it better for your application.....

This is the index.php file, which send the data for checking to another file.....

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Serialized Data</title>
<script type="text/javascript" src="../js/jquery.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    var x=$("#loading");
    x.hide();
    $("#latter form").submit(function(){
        //var x=$(this).serialize();
        //alert(x);
        x.ajaxStart(function() {
            $(this).show();
            $("#ff").hide('slow');
        });
        x.ajaxStop(function() {
            $(this).hide();
            $("#ff").show('slow');
        });
        $.post('result.php',$(this).serialize(), function(result){
            $("#rslt").html(result);
        });
        return false;
    });
});

</head>

<body>
<div id="loading">Loading...</div>
<div id="latter">
<form id="ff">
Name : <input type="text" id="txtName" name="txtName" />
Email :<input type="email" id="email" name="email" />
<input type="submit" value="Submit" name="submit" id="submit" />
</form>
</div>
<div id="rslt">

</div>
</body>
</html>

  • jQuery's serialize() method  make very easy to submit form data without any concern. It's send data using query string formal like -

          txtName=value&email=value
         so you dont have to access all the form  element to send it...
  •  In the "Loading...." section you can use any .gif image for batter animation.



Here is the response file, where data will be checked...I just display the data....You can check against database.....   

<?php
if(isset($_REQUEST['txtName']) && isset($_REQUEST['email']))
{
$name=$_REQUEST['txtName'];
$email=$_REQUEST['email'];
echo $name;
echo "<br/>";
echo $email;
}
else
{
    echo "Enter Your Name";
}
?>



Remember this is the very basic example for your Loading animation....You can make batter with this concept for your application.... 

PDO Prepared Statements with Parameters


A query intended for use as a prepared statement looks a bit different from those you might be used
to because placeholders must be used instead of actual column values for those that will change across
execution iterations. Two syntax variations are supported, named parameters and question mark
parameters. For example, a query using named parameters might look like this:

INSERT INTO products SET sku = :sku, name = :name;

The same query using question mark parameters would look like this:

INSERT INTO products SET sku = ?, name = ?;


 I will show you how to work with both.....

To bind the values to their respective variable name or positional offset in the query using the bindParam() method.

Here is the database connection file : 

db.inc.php 

<?php
try{
$db=new PDO("mysql:host=host_name;dbname=Database_name","Your_name","Password");
}
catch(PDOException $exception)
{
    printf("Connection Error..");
}
?>


Question mark parameters :
 
<?php
include_once("db.inc.php");
$query="insert into logins set username=? , pswd=?";
//
//Insert statement
//
$stmt=$db->prepare($query);
$name="HHH";
$pswd="123";
//binding parameter
$stmt->bindParam(1,$name);
$stmt->bindParam(2,$pswd);
$i=$stmt->execute();
if($i >0)
{
    echo "Value inserted";
}
else
{
    echo "Not Inserted";
}
?>


Name parameters :

<?php
include_once("db.inc.php");
$query="insert into logins set username=:user,pswd=:pswd";
//
//Insert statement
//
$stmt=$db->prepare($query);
$name="John";
$pswd="123";
//bind the parameter
$stmt->bindParam(':user',$name);
$stmt->bindParam(':pswd',$pswd);
//execute the query
$i=$stmt->execute();
if($i >0)
{
    echo "Value inserted";
}
else
{
    echo "Vlaue Not inserted";
}
?>

PHP database connection using PDO

PHP data access using PDO

PDO(PHP Data Object) is the secure process to access the database(like ADO.NET in .NET).

The database connection file :-

dp.inc.php

<?php
try{
$db=new PDO("mysql:host=Your_Host_Name;dbname=db_name","Your_User_Name","Password");
}
catch(PDOException $exception)
{
    printf("Connection Error..");
}
?>


To Delete data :-
delete.php

<?php
include_once("db.inc.php");
echo $query="delete from table_name whereField_name='value'";
$i=$db->exec($query);
if($i > 0)
{
    echo "<script>alert('Value Deleted :".$i."')</script>";
}
else{
    echo "<script>alert('No Result found...')</script>";
}
?>

To insert to the database :-

insert.php
<?php
include_once("db.inc.php");
$pswd=uniqid(rand());
$query="insert into Table_Name values('value1','value2','value2')";
$affect=$db->exec($query);
if($affect > 0)
{
    echo "<script>alert('Value inserted')</script>";
}
else
{
    echo "<script>alert('Error')</script>";
}


?>

Select Query :-
select.php

<?php
include_once("db.inc.php");
$query="select * from table_name";
$stmt=$db->query($query);
while($row= $stmt->fetch(PDO::FETCH_ASSOC))
{
    echo $row['id'];
    echo "<br/>";
    echo $row['username'];
    echo "<br/>";
    echo $row['pswd'];
    echo "<br/>";
}

?>

ASP.NET database Connection using web.config file

The simplest way to access database


When you create a Connection object, you can pass the connection string as a constructor
parameter. Alternatively, you can set the ConnectionString property by hand, as long as you do it before
you attempt to open the connection.
There’s no reason to hard-code a connection string.
The
<connectionStrings> section of the web.config file is a handy place to store your connection strings.

Example-
web.config file :-

<configuration>
<connectionStrings>
<add name="Northwind" connectionString=
"Data Source=localhost; Initial Catalog=Northwind; Integrated Security=SSPI"/>
</connectionStrings>
...
</configuration>

You can then retrieve your connection string by name from the
WebConfigurationManager.ConnectionStrings collection. Assuming you’ve imported the
System.Web.Configuration namespace, you can use a code statement like this:

public partial class _Default : System.Web.UI.Page
{
    string connectionString =
    WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString.ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
            
    }
}
Declare the connection string veritable  at the class label for later user

Data Access Process from SQL Server :-

protected void InsertData()
{
       string InsertSql = "insert into Table_Name values(value1,value2.....valueN)";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(InsertSql, con);
        int innn;
        try
        {
            con.Open();
            innn=cmd.ExecuteNonQuery();
            if (innn > 0)
            {
                Label1.Text = "Insert Successfull";
            }

        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
        finally
        {
            con.Close();
        }
}

Call this function from any Event Handler method like Button.Click[Buttion_Click(object sender, EventArgs e)] method....

This is the simple way to access database for DML or DQL execution. If you want you can get more complex execution with more security.....Like Parametrized Query and Query Builder object.......

If you need help please post your request......