Wednesday, 5 March 2014

PHP Server side validation

index.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=utf-8" />
<title>Untitled Document</title>



</head>

<body>

<?php
include("connect.php");
function test_input($data)
{
$data=trim($data);
$data=stripslashes($data);
$data=htmlspecialchars($data);
return $data;
}

$nameErr=$emailErr=$phoneErr="";
$myname=$myemail=$myphone="";

if($_SERVER['REQUEST_METHOD']=='POST')
{
$flag=1;
if(empty($_POST['name']))
{

$nameErr="* Name is required";
}
else
{
$myname=test_input($_POST['name']);
if(!preg_match("/^[a-zA-Z]*$/",$myname))
{
$flag=0;
$nameErr="* Only letters and white space";
}

}


if(empty($_POST['email']))
{

$emailErr="* email is required";
}
else
{
$myemail=test_input($_POST['email']);
if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$myemail))
{
$flag=0;
$emailErr="* Invalid email id";
}
}


if(empty($_POST['phone']))
{

$phoneErr="* Phone is required";
}
else
{
$myphone=test_input($_POST['phone']);
}
if(($flag==1)&& ($myname!="")&& ($myemail!="")&& ($myphone!=""))
{
$query=mysql_query("INSERT INTO myself VALUES ('','$myname','$myemail','$myphone')");
}


}

?>
<form method="post">
Name:<input name="name" type="text" /><?php echo $nameErr;?><br />
Email:<input name="email" type="text" /><?php echo $emailErr;?><br />
Phone:<input type="text" name="phone" /><?php echo $phoneErr;?><br />
<input type="submit" value="check" name="sub" />

</form>

</body>
</html>

Tuesday, 25 February 2014

Pagination in php

<html>
<body>
<?php
error_reporting(0);
include('connect.php');
$start=$_REQUEST['start'];
if(empty($start))
{
$start=0;
}
?>
<table align="center" border="0">
        <tr>
                <td width="30">Sl.No</td>
                <td width="100">Name</td>
        </tr>
<?php
if(!isset($start))  
{
$start=0;
}
$limit=3;
$sql=mysql_query("SELECT * FROM vin_pagination LIMIT ".$start." , ".$limit."");
while($fetch=mysql_fetch_array($sql))
{
?>
        <tr>
                <td>
                  <?php echo $fetch['p_id'];?>
                </td>
                <td>
                <?php echo $fetch['p_name'];?>
                </td>
</tr>
        <?php
        }
?>
<tr>
                <td colspan="2" align="center">
                <?php
                $p_sql=mysql_query("SELECT * FROM  vin_pagination");
                $count=mysql_num_rows($p_sql);
                        $prev=$start-$limit;
                        $next=$start+$limit;
                        if($prev>=0)
{
                        echo "<a href=\"index.php?start=$prev\" style=text-decoration:none;color:#999999>Prev</a>&nbsp;&nbsp;";
                        //displaly number of pages
                       
                        }
                        $i=0;
                        $j=1;
                        for($i=0;$i<$count;$i=$i+$limit)
                        {
                                if($i!=$start)
                                {
                                    echo "<a href=\"index.php?start=$i\" style=text-decoration:none;color:#999999>$j</a>&nbsp;&nbsp;";
                                }
                                else
                                {
                                    echo  "<a href=\"index.php?start=$i\" style=text-decoration:none><font color=brown>$j</font></a>&nbsp;&nbsp;";
                                }
                                $j++;
                        }
                        if($count>$next)
{
                        echo "<a href=\"index.php?start=$next\" style=text-decoration:none;color:#999999>Next</a>&nbsp;&nbsp;";
                        }
       
        ?>
<td>      
    </tr>
</table>
</body>
</html>

Tuesday, 18 February 2014

Dynamic Check box inserting and updating using implode and explode functions

<form  method="post">
Hobbies:
    <?php
include('connect.php');
$sql=mysql_query("SELECT * FROM olx_area ");
$cnt=mysql_num_rows($sql);

while($fcat=mysql_fetch_array($sql))
{?>
            <input type="checkbox" name="hobbies[]" value="<?php echo $fcat['area_name'];?>"><?php echo $fcat['area_name'];?><br>
            <?php }?>
           
<input type="submit" name="sub" value="save">
</form>
<?php
if(isset($_POST['sub']))
{
$nhobbies=$_POST['hobbies'];
$imp=implode(", ",$nhobbies);
$sqlq = mysql_query("insert into form values('','$imp')");
}
?>

<form method="post">
enter id :<input type="text" name="id">
<input type="submit" name="edit" value="edit">
</form>
<?php
include('connect.php');
if(isset($_POST['edit']))
{
$eid=$_POST['id'];
$qwe=mysql_query("SELECT * FROM form WHERE id=$eid" );
$fet=mysql_fetch_array($qwe);

$string="$fet[hobbies]";
echo $string;
$array=explode(', ', $string);
$result = count($array);
$res=$result-1;
echo"Total counting="; echo $result;echo "<br>";
 for($i=0;$i<=$res;$i++)
 {
 echo "$array[$i]";
 }

 
         


$sql=mysql_query("SELECT * FROM olx_area ");



while($fcat=mysql_fetch_array($sql))
{?>
            <input type="checkbox" name="hobbies[]" value="<?php echo $fcat['area_name'];?>"
           
            <?php for($i=0;$i<=$res;$i++)
{
if($array[$i]==$fcat['area_name'])
{
echo "checked";
}

}
?>
         
            >
<?php echo $fcat['area_name'];?><br>
            <?php }
 }          
?>

Friday, 14 February 2014

Dynamic Check box inserting and updating using substr & for each function

<form  method="post">
Hobbies:
    <?php
include('connect.php');
$sql=mysql_query("SELECT * FROM olx_area ");
$cnt=mysql_num_rows($sql);

while($fcat=mysql_fetch_array($sql))
{?>
            <input type="checkbox" name="hobbies[]" value="<?php echo $fcat['area_name'];?>"><?php echo $fcat['area_name'];?><br>
            <?php }?>
         
<input type="submit" name="sub" value="save">
</form>
<?php
if(isset($_POST['sub']))
{
$nhobbies=$_POST['hobbies'];
$nselected_hobbies= "";
foreach($nhobbies as $nhobbie)
{
$nselected_hobbies .= $nhobbie . ", ";
}
$nselected_hobbies = substr($nselected_hobbies,0,-2);
$sqlq = mysql_query("insert into form values('','$nselected_hobbies')");

}
?>

<form method="post">
enter id :<input type="text" name="id">
<input type="submit" name="edit" value="edit">
</form>
<?php
include('connect.php');
if(isset($_POST['edit']))
{
$eid=$_POST['id'];
$qwe=mysql_query("SELECT * FROM form WHERE id=$eid" );
$fet=mysql_fetch_array($qwe);

$string="$fet[hobbies]";
echo $string;
$array=explode(', ', $string);
$result = count($array);
$res=$result-1;
echo"Total counting="; echo $result;echo "<br>";
 for($i=0;$i<=$res;$i++)
 {
 echo "$array[$i]";
 }


         


$sql=mysql_query("SELECT * FROM olx_area ");



while($fcat=mysql_fetch_array($sql))
{?>
            <input type="checkbox" name="hobbies[]" value="<?php echo $fcat['area_name'];?>"
         
            <?php for($i=0;$i<=$res;$i++)
{
if($array[$i]==$fcat['area_name'])
{
echo "checked";
}

}
?>
         
            >
<?php echo $fcat['area_name'];?><br>
            <?php }
 }        
?>

Monday, 6 January 2014

Simple form insertion, deletion, updation using jquery

Index.php




<html>
<head>
<style>

#popup_box,#popup_box1,#popup_box2,#popup_box3,#popup_box4 {
display:none;    
position:fixed;      
_position:absolute;      
height:300px;      
width:600px;      
background:#95CAFF;
left: 300px;    

z-index:100;    
border:2px solid #63b8ee;          
 font-size:15px;      
 -moz-box-shadow: 0 0 5px #0066FF;    
 -webkit-box-shadow: 0 0 5px #0066FF;  
  box-shadow: 0 0 5px #0066FF;    
  }

</style>
<script src="jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready( function() {


$('#popupBoxClose').click( function() {                        
unloadPopupBox();        
});

function unloadPopupBox()
{                
$('#popup_box').fadeOut("slow");            
$("#container").css({                        
"opacity": "1"              
});        
}          












$('#add').click( function()
{            
loadPopupBox();        
});


function loadPopupBox()
{              
$('#popup_box').fadeIn("slow");            
$("#container").css({                  "opacity": "0.3"               });                
}












$('#addbutton').click(function()
{
var name=$('#nameadd').val();
var address=$('#addressadd').val();
$.get("check.php",{Name:name,Address:address,operation:"insert"},function(data)
{

});
loadPopupBox3();  

});


function loadPopupBox3()
{              
$('#popup_box3').fadeIn("slow");            
$("#container").css({                  "opacity": "0.3"               });                
}






$('#addpopup').click(function()
{
unloadPopupBox3();    
});

function unloadPopupBox3()
   {                
$('#popup_box3').fadeOut("slow");            
$("#container").css({                        
"opacity": "1"              
});        
}








$('.edit').click( function() {  

var id = $(this).attr("id");

$.get("check.php",{Slno:id,operation:"updateselect"},function(data)
{
$("#registration").remove();
$("#popup_box1").append(data);
});
 
loadPopupBox1();    
});

function loadPopupBox1()
{              
$('#popup_box1').fadeIn("slow");            
$("#container").css({                  "opacity": "0.3"               });                
}
                 






$('#save').click(function(){

var idsave=$('#idsave').val();
var name=$('#namesave').val();
var address=$('#addresssave').val();
$.get("check.php",{Id:idsave,Name:name,Address:address,operation:"update"},function(data)
{

});
loadPopupBox4();    
});
function loadPopupBox4()
{              
$('#popup_box4').fadeIn("slow");            
$("#container").css({                  "opacity": "0.3"               });                
}







$('#updatepopup').click(function()
      {
unloadPopupBox4();    
});

function unloadPopupBox4() {                
$('#popup_box4').fadeOut("slow");            

$("#container").css({                        
"opacity": "1"              
});        
}  













$('.delete').click( function() {            
var id = $(this).attr("id");
//alert(id);
$.get("check.php",{Slno:id,operation:"delete"},function(data)
{//2
$("#registration").remove();
$("#popup_box2").append(data);
});//2
 
loadPopupBox2();  
});



function loadPopupBox2()
{              
    $('#popup_box2').fadeIn("slow");            
$("#container").css({                  "opacity": "0.3"               });                
}





});



</script>
</head>
<body>
   <div id="container">
   <button type="button" class="myButton1" id="add" >Add</button>
 
   <div id="grid">
   <?php
  include_once 'dbcon.php';
connect();
$sql_grid=mysql_query("select * from tab");
echo "<table border=1>
                         <tr><th>ID</th><th>Name</th><th>Address</th><th>Action1</th><th>Action2</th></tr>";
while($test_grid=mysql_fetch_array($sql_grid))
{
                        echo"<tr id=".$test_grid['id']." class='sp'><td>".$test_grid['id']."</td><td>".$test_grid['name']."</td><td>".$test_grid['address']."</td><td><img src='img\edit.png' width='20' height='20' class='edit' id='".$test_grid['id']."'/></td><td><img src='img\del.jpg' width='20' height='20' class='delete' id='".$test_grid['id']."'/></td></tr>";
                  }
   echo "</table>";

?>
                    </div>
                    </div>
                   

 
 
 
   </div>




<form method="post">
<div id="popup_box" >    <!-- OUR PopupBox DIV-->
<a id="popupBoxClose"><button type="submit">X</button></a>

    <div id="registration" style="width:700px;">
    <h2>Account Detail</h2>
    <form  method="post">
    <center>
    Name:<input type="text" id="nameadd" class="text" value=""/><br>
    Address:<input type="text" id="addressadd" class="text" value=""/><br>
    <button  id="addbutton" type="button" >Add</button>
    </center>
    </form>
                </div>
   
    </div>
    </form>
   
   
   
    <div id="popup_box3">    <!-- OUR PopupBox DIV-->
                                                <a id="popupBoxClose3"><button type="submit" >X</button></a>
        <div id="registration" style="width:700px;">
  <h2>Account Detail</h2>
<form method="post">
<h3>Data Added Successfully</h3>
            <button type="submit"  id="addpopup" >Ok</button>
</form>
</div>

   </div>
 
 
 
 
 
 
   <form method="post">
<div id="popup_box1" >    <!-- OUR PopupBox DIV-->
<a id="popupBoxClose1"><button type="submit">X</button></a>
       
               <div >
               <button class='myButton1' id='save' type='button' >Save</button>
               </div>
</div>
</form>
   
   
   
   
   
    <div id="popup_box4" >    <!-- OUR PopupBox DIV-->
<a id="popupBoxClose5"><button type="submit" >X</button></a>
    <div id="registration" >
<h2>Account Detail</h2>
<form id="RegisterUserForm"  method="post">
<center>
<h3>Data Updated Successfully</h3>
            <button type="submit" class="myButton1" id="updatepopup" >Ok</button>
</center>
</form>
</div>

</div>

<form method="post">
<div id="popup_box2" style="left:400px; top:200px; width:700px;">    <!-- OUR PopupBox DIV-->
<a id="popupBoxClose2"><button type="submit" style="width:20px; background:#009900; color:#FFFFFF; border-radius:5px; height:20px; margin-left:670px;">X</button></a>
     
</div>
</form>
   
   

</body>
</html>


____________________________________________________________________________





check.php






<?php
error_reporting(0);
if(isset ($_GET['operation']) && $_GET['operation']!='')
{
include_once 'dbcon.php';

connect();
    $op=$_GET['operation'];
if($op=='insert')
{
$name_add=$_GET['Name'];
$address_add=$_GET['Address'];
$sql_insert=mysql_query("insert into tab values('','$name_add','$address_add')");
}
else if($op=='updateselect')
{
$Slno=$_GET['Slno'];
$sql_select=mysql_query("select * from tab where id='$Slno'");
while($test_select=mysql_fetch_array($sql_select))
{
$name_select=$test_select['name'];
$address_select=$test_select['address'];
}
echo "<div id='registration'>";
        echo "<h2>Account Detail</h2>";
        echo "<form id='RegisterUserForm'  method='post'>";
        echo "<center>";
        echo "<table>";
        echo "<tr>";
        echo "<td><input type='hidden' id='idsave'  class='text' value='$Slno'/></td><td></td>";
        echo "</tr>";
       echo "<tr>";
       echo "<td class='lbltext'><label>Name</label></td><td><input type='text' id='namesave' class='text'  value='$name_select'/></td><td></td>";
       echo "</tr>";
       echo "<tr>";
       echo "<td class='lbltext'><label>Address</label></td><td><input type='text' id='addresssave' class='text' ' value='$address_select'/></td><td></td>";
    echo "</tr>";
    echo "<tr>";
echo "</table>";
    echo "</center>";
    echo "</form>";
    echo "</div>";

}
else if($op=='update')
{
$Id_save=$_GET['Id'];
$Name_save=$_GET['Name'];
$Address_save=$_GET['Address'];
$sql_update=mysql_query("update tab set name='$Name_save',address='$Address_save' where id='$Id_save'");
}
else if($op=='delete')
{
$Slno=$_GET['Slno'];
$sql_delete=mysql_query("delete from tab where id='$Slno'");
if($sql_delete)
{
echo "<div id='registration' style='width:700px;'>";
echo "<h2>Account Detail</h2>";
echo "<form id='RegisterUserForm'  method='post'>";
echo "<center>";
echo "<h3>Data Deleted Successfully</h3>";
echo "</center>";
echo "</form>";
echo "</div>";
}
else
{
echo "<div id='registration' style='width:700px;'>";
echo "<h2>Account Detail</h2>";
echo "<form id='RegisterUserForm'  method='post'>";
echo "<center>";
echo "<h3>Data Not Deleted</h3>";
echo "</center>";
echo "</form>";
echo "</div>";

}
}
}

?>


_________________________________________________________________________



dbcon.php




<?php
function connect()
{
$server = 'localhost';
$uname = 'root';
$password = '';
$dbname = 'example';

mysql_connect($server, $uname, $password)
or die();

mysql_select_db($dbname);
}
?>





________________________________________________________________

Friday, 27 December 2013

Insert form datas in database using jQuery

index.php



<html>
<head>



<script src="jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
alert('hai');

$(".sub").click(function(){
alert('hhh');

var vname=$("#iname").val();
var vmob=$("#imob").val();
alert(vname);
alert(vmob);



$.get("test.php",{Name:vname,Mob:vmob,operation:"Sub"},function(data)
{

alert( "insert successfully");
           
        });
});
});
</script>





</head>
<body>

   
    <form method="post">
Name:<input type="text" name="name" id="iname"><br>
Fees:<input type="text" name="mob" id="imob"><br>
 
    <input type="button" class="sub" name="sub" value="Submit">
  </form>


</body>
</html>





__________________________________________________________________________




test.php




<?php

if(isset ($_GET['operation']) && $_GET['operation']!='')
{
    include_once 'connect.php';
$op=$_GET['operation'];

if($op=='Sub')
{
$Name=$_GET['Name'];
$Mob=$_GET['Mob'];

$sql = mysql_query("insert into phone values('','$Name','$Mob')");
}
}


?>






___________________________________________________________________________





connect.php





<?php
$con=mysql_connect('localhost','root','');
mysql_select_db('sinsert',$con);
?>






_____________________________________________________________________

Insert Dynamic Check box in database using jQuery

index.php




<html>
<head>
<script src="jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
alert('hai');
$(".sub").click(function(){
alert('hhh');
var pcheck="";
$(".pack").each(function(){
var id=$(this).attr("id");
if ($('#'+id).is(':checked')) 
{
pcheck+=$('#'+id).val()+","; 
}
});
alert(pcheck);
$.get("test.php",{Subject:pcheck,operation:"Sub"},function(data)
{
alert( "insert successfully");
             
        });
});
});
</script>





</head>
<body>


<form method="post">
  Subject:<br>
  <input type="checkbox" class="pack" name="subject[]"  value="English" id="1">English <br>
        <input type="checkbox" class="pack" name="subject[]"  value="Tamil" id="2">Tamil <br>
        <input type="checkbox" class="pack" name="subject[]"  value="Maths" id="3">Maths <br>
 
 
 
  
    <input type="button" class="sub" name="sub" value="Submit">
    
  
</form>


</body>
</html>



______________________________________________________________________________



test.php





<?php

if(isset ($_GET['operation']) && $_GET['operation']!='')
{
    include_once 'connect.php';
$op=$_GET['operation'];

if($op=='Sub')
{
$Subject=$_GET['Subject'];
$sql = mysql_query("insert into subject values('','$Subject')");
}
}

?>






______________________________________________________________________________


connect.php




<?php
$con=mysql_connect('localhost','root','');
mysql_select_db('dynamiccheck',$con);
?>





________________________________________________________________________________