<?
# เผยแพร่ใน http://www.thaiall.com/perlphpasp/source.pl?9138
# ปรับปรุง 19 กันยายน 2553
# ===
# ส่วนกำหนดค่าเริ่มต้นของระบบ
$host = "localhost";
$db = "test";
$tb = "test"; // create table test (id varchar(20), ns varchar(20), salary varchar(20));
$user = "admin"; // รหัสผู้ใช้ ให้สอบถามจากผู้ดูแลระบบ
$password = "p"; // รหัสผ่าน ให้สอบถามจากผู้ดูแลระบบ
$conn = mysql_connect("$host","$user","$password");
if (isset($_REQUEST{'action'})) $act = $_REQUEST{'action'}; else $act = "";
# ส่วนเพิ่มข้อมูล
if ($act == "add") insert_data();
if ($act == "del_confirm") delete_data();
if ($act == "update_confirm") update_data();
if ($act == "select" || $act == "del" || $act == "update") select_data();
else echo "<textarea rows=5 cols=60></textarea><br/>";
# ajax ?>
<script type="text/javascript">
function ajax(act,eid,ns,salary) {
var xmlHttp;
var xdate = new Date();
try { // Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
} catch (e) {
try { // Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function() {
if(xmlHttp.readyState==4) {
document.getElementById('area').innerHTML=xmlHttp.responseText;
}
}
var url = "?action=" + act + "&eid=" + eid + "&ns=" + ns + "&salary=" + salary;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
</script>
<div id="area"></div>
<script>ajax('select','','','');</script>
<?
exit;
function delete_data() {
global $host, $db, $tb, $user, $password,$act,$conn;
$q = "delete from test where id ='". $_REQUEST{'eid'} . "'";
$conn = mysql_connect("$host","$user","$password");
$r = mysql_db_query($db,$q);
mysql_close($conn);
select_data();
exit;
}
function update_data() {
global $host, $db, $tb, $user, $password,$act,$conn;
$q = "update $tb set ns ='". $_REQUEST{'ns'} . "', salary ='". $_REQUEST{'salary'} . "' where id =" . $_REQUEST{'eid'};
$conn = mysql_connect("$host","$user","$password");
$r = mysql_db_query($db,$q);
mysql_close($conn);
select_data();
exit;
}
function insert_data() {
global $host, $db, $tb, $user, $password,$act,$conn;
$q = "insert into test values('". $_REQUEST{'eid'} . "','". $_REQUEST{'ns'} . "','". $_REQUEST{'salary'} . "')";
$conn = mysql_connect("$host","$user","$password");
$r = mysql_db_query($db,$q);
mysql_close($conn);
select_data();
exit;
}
function select_data() {
global $host, $db, $tb, $user, $password,$act,$conn;
$q = "select * from $tb";
$rs = mysql_db_query($db,$q);
echo "<table>";
while ($rs && $o = mysql_fetch_object($rs)) {
if (isset($_REQUEST{'eid'}) && $_REQUEST{'eid'} == $o->id && strlen($act) < 7 && $act != "add") $chg = " style='background-color:#f9f9f9"; else $chg = " readonly style='background-color:#ffffdd";
echo "<tr><form action='' method=post>
<td><input name=eid size=5 value='". $o->id . "' style='background-color:#dddddd' readonly></td>
<td><input name=ns size=40 value='". $o->ns . "' $chg'></td>
<td><input name=salary size=20 value='". $o->salary . "' $chg;text-align:right'></td>
<td>";
if (isset($_REQUEST{'eid'}) && $_REQUEST{'eid'} == $o->id && strlen($act) < 7 && $act != "add") {
if ($act == "del") echo "<input type=button name=action value='del_confirm' style='height:40;background-color:yellow' onclick='javascript:ajax(\"del_confirm\",eid.value,ns.value,salary.value)'>";
if ($act == "update") echo "<input type=button name=action value='update_confirm' style='height:40;background-color:#aaffaa' onclick='javascript:ajax(\"update_confirm\",eid.value,ns.value,salary.value)'>";
} else {
echo "<input type=button name=action value='del' style='height:26' onclick='javascript:ajax(\"del\",eid.value,ns.value,salary.value)'> <input type=button name=action value='update' style='height:26' onclick='javascript:ajax(\"update\",eid.value,ns.value,salary.value)'>";
}
echo "</td></form></tr>";
}
echo "<tr><form action='' method=post><td><input name=eid size=5></td><td><input name=ns size=40></td><td><input name=salary size=20></td><td><input type=button name=action value='add' style='height:26' onclick='javascript:ajax(\"add\",eid.value,ns.value,salary.value)'></td></tr></form></table>";
exit;
}
?>
จำนวน : 107 บรรทัด