PHP+Jquery 를 기반 으로 편집 가능 한 표 의 코드

4706 단어 편집 가능표.
table.php
 
<?php
header("Content-Type:text/html;charset=utf-8");
$mysqli=new MySQLi("localhost","root","123456","xiaoqiangdb");
if(mysqli_connect_errno){
echo " ".mysqli_connect_error();
exit;
}
?>
<html>
<head>
<title> </title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="jquery-1.3.2.min.js"></script>
<script src="table.js"></script>
</head>
<body>
<?php
$sql="select id,name,age,sex,email from users limit 0,20";
$result=$mysqli->query($sql);
echo "<table>";
echo "<caption> </caption>";
echo "<tr>";
echo "<th> </th><th> </th><th> </th><th> </th><th> </th>";
echo "</tr>";
while($row=$result->fetch_assoc()){
echo '<tr>';
echo '<td class="id">'.$row['id']'</td>';
echo '<td>'.$row['name']'</td>';
echo '<td>'.$row['age']'</td>';
echo '<td>'.$row['sex']'</td>';
echo '<td>'.$row['email']'</td>';
echo '</tr>';
}
echo "</table>";
$mysqli->close();
?>
</body>
</html>
style.css
 
@charset "utf-8";
/* CSS Document */
body{ font-size:12px; background:#EEE; text-align:center;}
table{ width:600px; border:1px solid #050; border-collapse:collapse;}
th,td{ border:1px solid #050; width:120px;}
th{ background:#282; color:white;}
table.js
 
// JavaScript Document
$(function(){
$("tr:even").css("background-color","#ffff99");
$("tr td:not(.id)").click(function(){
if($(this).children('input').length > 0)
return;
//
var data=$(this).text();
//
$(this).html('');
var td=$(this);
//
var inp=$('<input type="text">');
inp.val(data);
inp.css("background-color",$(this).css("background-color"));
inp.css("border-width","0px");
inp.css("width",$(this).css("width"));
// input
inp.appendTo($(this));
//
inp.trigger('focus');
//
inp.trigger('select');
//
inp.keydown(function(event){
switch(event.keyCode){
case 13:
td.html($(this).val());
// Ajax
//
var tds=td.parent("tr").children("td");
var i=tds.eq(0).text();
var n=tds.eq(1).text();
var a=tds.eq(2).text();
var s=tds.eq(3).text();
var e=tds.eq(4).text();
//var user={id:i,name:n,age:a,sex:s,email:e}
$.post("save.php",{id:i,name:n,age:a,sex:s,email:e},function(data){
alert(data);
});
break;
case 27:
td.html(data);
break;
}
}).blur(function(){
td.html($(this).val());
// Ajax
//
var tds=td.parent("tr").children("td");
var i=tds.eq(0).text();
var n=tds.eq(1).text();
var a=tds.eq(2).text();
var s=tds.eq(3).text();
var e=tds.eq(4).text();
//var user={id:i,name:n,age:a,sex:s,email:e}
$.post("save.php",{id:i,name:n,age:a,sex:s,email:e},function(data){
alert(data);
});
});
});
});
save.php
 
<?php
header("Content-Type:text/html;charset=utf-8");
$mysqli=new MySQLi("localhost","root","123456","xiaoqiangdb");
if(mysqli_connect_errno){
echo " ".mysqli_connect_error();
exit;
}
$sql="update users set name='{$_POST["name"]}',age='{$_POST["age"]}',sex='{$_POST["sex"]}',email='{$_POST["email"]}' where id='{$_POST["id"]}'";
$result=$mysqli->query($sql);
if($result){
echo " ";
}else{
echo " ";
}
$mysqli->close();
?>

좋은 웹페이지 즐겨찾기