Friday, February 4, 2011

ajax insert,update,delete

create action.php

<?php
 require('common/dbconnect.php');
?>

<!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=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
  function delete(){
    var xmlhttp;
    if(window.XMLHttpRequest){
      xmlhttp=new XMLHttpRequest();
    }
    else{
      xmlhttp=new ActiveXObject("Microsoft.XMLHttp");
    }
    xmlhttp.open("GET","delete.php",true);
    xmlhttp.send();
    xmlhttp.onreadystatechange=function()
    {
     if(xmlhttp.readyState==4 && xmlhttp.status==200){
      document.getElementById("custdiv").innerHTML=xmlhttp.responseText;
    }
    }
  }
 
</script>
</head>

<body>
 <form action="" method="post">
 
   <div id="custdiv"></div>
   <input type="button" value="add" name="add" onclick="javascript:location.href='add.php'" />
    <table border="1" bordercolor="#3399FF">
    <tr bgcolor="#CCFFCC">
      <th> Sno </th>
      <th> Cust Name </th>
      <th> Cust Father </th>
      <th> Address </th>
      <th> Actions </th>
      <th></th>
     </tr>
    <?php
      $i=1;
      //$res=mysql_query("select custid,custname,custfather,address from customer") or die(mysql_error());
      $res=mysql_query("select * from customer");
        while($row=mysql_fetch_array($res)){
    ?>
     <tr>
      <td> <input type="checkbox" name="sno" value="<?php echo $i?>" /><?php echo $i?></td>
      <td>
     <?php echo $row['custname']; ?>
      </td>
      <td>
     <?php echo $row['custfather']; ?>
      </td>
      <td>
     <?php echo $row['address']; ?>
      </td>
      <td> <a href="update.php?cid=<?php echo $row['custid']; ?>">Update</a></td>
      <td><a href="delete.php?cid=<?php echo $row['custid']; ?>" >Delete</a></td>
     </tr>
     <?php $i++; } ?>
    </table>
 </form>
</body>
</html>

create add.php

<!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=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">

 function addcust(){
   //alert("hai"); 
   str1=document.getElementById("cname").value;
   str2=document.getElementById("cfname").value;
   str3=document.getElementById("address").value;
   if(str1==""||str2==""||str3==""){
    alert("please add the fields");
    return false;
   }
  
   var xmlhttp;
    if(window.XMLHttpRequest){
      xmlhttp=new XMLHttpRequest();
    }
    else{
      xmlhttp=new ActiveXObject("Microsoft.XMLHttp");
    }
    url="addcust.php?name="+str1+"&fname="+str2+"&addres="+str3;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
    xmlhttp.onreadystatechange=function()
    {
     if(xmlhttp.readyState==4 && xmlhttp.status==200){
      document.getElementById("custdiv").innerHTML=xmlhttp.responseText;
    }
    }
  return false;
 }
</script>
</head>

<body>
   <div id="custdiv">
  <table border="1">
    <tr>
     <td> Customer Name </td>
     <td><input type="text" name="cname" id="cname" value="" /></td>
    </tr>
    <tr>
     <td> Customer Father Name </td>
     <td> <input type="text" name="cfname" id="cfname" value=""/></td>
    </tr>
    <tr>
     <td> Address </td>
     <td> <input type="text" name="address" id="address" value="" /></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" value="submit" onclick="addcust()" /></td>
    </tr>
    </table>
   </div>
</body>
</html>


addcust.php


<?php
  $name=$_GET["name"];
  $fname=$_GET["fname"];
  $address=$_GET["addres"];
  require("common/dbconnect.php");
  mysql_query("insert into customer(custname,custfather,address) values('$name','$fname','$address')") or die(mysql_error());
        header("Location:actions.php");
?>

update.php


<?php
    require("common/dbconnect.php");
        $cid=$_GET["cid"];
?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
 function update(){
   var xmlhttp;   
   cid=document.getElementById("cno").value;
   str1=document.getElementById("cname").value;
   str2=document.getElementById("cfname").value;
   str3=document.getElementById("address").value;
   //alert(str1);
   //alert(str2);
   //alert(str3);
   if(str1==""||str2==""||str3==""){
     alert("insert any value not empty");
     return false;
   }
   if(window.XMLHttpRequest){
     xmlhttp = new XMLHttpRequest();
   }
   else{
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
   url = "edit.php?name="+str1+"&fname="+str2+"&addr="+str3+"&cno="+cid;
   xmlhttp.open("GET",url,true);
   xmlhttp.send(null);
   xmlhttp.onreadystatechange=function()
   {
     if(xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementById("cdiv").innerHTML=xmlhttp.responseText;
        //document.getElementById("cdiv").style.display="none";
     }
   }
  return false; 
}
</script>
</head>

<body>

 <?php
      $res=mysql_query("select * from customer where custid=$cid");
    $row=mysql_fetch_array($res);
  ?>
  <div id="cdiv">
  
   <input type="hidden" id="cno" name="cno" value="<?php echo $cid?>" />
     <table border='1'>
    <tr>
      <td>Customer Name</td>
      <td><input type="text" name="cname" id="cname" value="<?php echo $row['custname']?>" /></td>
    </tr>
    <tr>
      <td>Customer Father Name</td>
      <td><input type="text" name="cfname"  id="cfname" value="<?php echo $row['custfather']?>" /></td>
    </tr>
    <tr>
      <td>Address</td>
      <td><input type="text" name="address"  id="address" value="<?php echo $row['address']?>"/></td>
    </tr>
    <tr>
      <td></td>
      <td><button type="submit" onclick="update()">update</button></td>
    </tr>
   </table>
  </div>
 </body>
</html>



edit.php


<?php
  $cid=$_GET["cno"];
  $name=$_GET["name"];
  $fname=$_GET["fname"];
  $addr=$_GET["addr"];
  require('common/dbconnect.php');
  mysql_query("update customer set custname='$name',custfather='$fname',address='$addr' where custid=$cid")or die("error".mysql_error());
  header("Location:actions.php");
?>

delete.php

<?php
  require("common/dbconnect.php");
  $cno=$_GET["cid"];
  mysql_query("delete from customer where custid=$cno");
  header("Location:actions.php");
?>


dbconnect.php

<?php
  $con=mysql_connect("localhost","root","") or die("fail to connect".mysql_error());
  mysql_select_db("base",$con) or die(mysql_error());
?>

                                                                

No comments:

Post a Comment