วันจันทร์ที่ 17 มกราคม พ.ศ. 2554

inline update by num


<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_GET['action'] == 'approve'){
$id = intval(@$_GET['id']);
echo 'update tb1 set APPROVE=1 where id='.$id;
//you will do real update yourself.
die;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_GET['action'] == 'unapprove'){
$id = intval(@$_GET['id']);
echo 'update tb1 set APPROVE=0 where id='.$id;
//you will do real update yourself.
die;
}
?>
<!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=tis-620" />
<title>inline update by num</title>
<style type="text/css">
* {margin:0;padding:0}
tr.checked {background-color:#ccc;}
#tb tr.orange {background-color:#F90;}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script>
//<![CDATA[
$(function(){
$('.approve:checked').parents('tr').addClass('checked');
$('.approve').click(function(e){
var tr = $(this).parents('.tr');
var record_id = $(this).val();
tr.addClass('orange');

//set to valid
if ($(this).is(':checked')){
if (confirm('set to valid?')){
$.ajax({ //ajax update
url:'?action=approve&id='+record_id,
type:'POST',
dataType:'html',
success:function(data){
alert(data);
tr.removeClass('orange').addClass('checked');
}
}); //end ajax update
}else{
e.preventDefault();
tr.removeClass('orange').removeClass('checked');
}
}

//set to invalid
if (!$(this).is(':checked')){
if (confirm('set to invalid?')){
$.ajax({ //ajax update
url:'?action=unapprove&id='+record_id,
type:'POST',
dataType:'html',
success:function(data){
alert(data);
tr.removeClass('orange').removeClass('checked');
}
}); //end ajax update
}else{
e.preventDefault();
tr.removeClass('orange').addClass('checked');
}
}

});
});
//]]>
</script>
</head>

<body>

<table border="1">
<tbody id="tb">
<tr class="tr"><td>###### 1</td><td><input type="checkbox" value="1" class="approve" /></td></tr>
<tr class="tr"><td>###### 2</td><td><input type="checkbox" value="2" class="approve" /></td></tr>
<tr class="tr"><td>###### 3</td><td><input type="checkbox" value="3" class="approve" checked="checked" /></td></tr>
</tbody>
</table>

</body>
</html>

ไม่มีความคิดเห็น:

แสดงความคิดเห็น