Tuesday 23 April 2013

CRUD operation using checkbox in PHP


<!--jquery for check box selecting  -->
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function ()
{
$('#d').click(function()
{
if (this.checked==true)
{
$('input[name="a[]"]').prop('checked', true);
}
if (this.checked==false)
{

$('input[name="a[]"]').prop('checked', false);
}
});
});
</script>
<!--new registeration form -->
Register here !!
<form action="" method="post">
username : <input type="text" name="uname"/></br>
password : <input type="password" name="pas"/></br>
Postion  :<input type="text" name="pos"/></br>
<input type="submit" value="register" name="reg"/>
</form>
<!--new chk box editing-->
<?php
$cn=mysql_connect("localhost","root","123");
mysql_select_db("hotel",$cn);
$wue=mysql_query("select * from user");
echo '<form action="" method="post">';
?>
<!--Displaying db values with check box-->
<table>

<tr>
<td><input type="checkbox" name="act" value="" id="d"></td>
<td>ID</td>
<td>USERNAME</td>
<td>POSITION</td>
</tr>
<?php
while($row=mysql_fetch_array($wue))
{
?>
<tr>
<td>
<input type="checkbox" name="a[]" value="<?php echo $row['user_id'];?>"></td>
<?php<
echo '<td>'.$row['user_id'].'</td>';
echo '<td>'.$row['username'].'</td>';
echo '<td>'.$row['position'].'</td>';
echo '</tr>';
}
?>
</table>
<?php
echo '<input type="submit" name="subval" value="edit"/>'.'<input type="submit" name="subdelete" value="delete"/>'.'</form>';
//check box selected value will be displayed in text box
if(isset($_POST['subval']))
{
$ar=$_POST['a'];
echo '<form action="" method="post">';
foreach($ar as $ty)
{
$ru=mysql_query("select * from user where user_id='$ty'");
$ew=mysql_fetch_array($ru);
?>
username :<input type="text" value="<?php echo $ew['username'];  ?>" name="name<?php echo $ty ?>"/>
Position :<input type="text" value="<?php echo $ew['position']; ?>" name="pos<?php echo $ty ?>"/>
<input type="hidden" name="id[]" value="<?php echo $ew['user_id']; ?>">
</br>
<?php
}
echo '<input type="submit" name="subupdate" value="update"/>'.'</form>';
}
//update the text box value after editing
if(isset($_POST['subupdate']))
{
$u=$_POST['id'];
foreach($u as $i)
{
$updt=mysql_query("update user set username='".$_POST['name'.$i]."' , position='".$_POST['pos'.$i]."' where user_id='$i'");
if($updt)
{
echo '<script>window.location=""</script>';
}
}
}
//delete the selected values by using check box
if(isset($_POST['subdelete']))
{
$uio=$_POST['a'];
foreach($uio as $yu)
{
$delt=mysql_query("delete from user where user_id='$yu'");
if($delt)
{
echo '<script>window.location=""</script>';
}
}
}
if(isset($_POST['reg']))
{
$insr=mysql_query("insert into user ( `username`, `password`, `position`) values('".$_POST['uname']."','".$_POST['pas']."','".$_POST['pos']."')");
if($insr)
{
echo '<script>window.location=""</script>';
}
}
?>

OUTPUT:
Download

No comments:

Post a Comment