我有下面的代码在单击时编辑单元格值,但是当用户单击保存按钮时,我希望新值存储在数据库中..请帮助我
<?php include"connect.php"; ?>
<html>
<head>
<title>Edit time records</title>
<style>
table{ text-align:justified;}
a {text-decoration:none;
color:black;
}
.replace {display:none;}
</style>
<link rel="stylesheet" type="text/css" href="css/allRecordsStyle.css">
<script type="text/javascript">
/*<![CDATA[*/
function INPUT(id){
var obj=document.getElementById(id),tds=obj.getElementsByTagName('SPAN'),z0=0,ip,html;
for (;z0<tds.length;z0++){
tds[z0].onmouseup=function(){ AddInput(this); }
}
}
function AddInput(td){
var ip=zxcAddField('INPUT','text','');
ip.value=td.innerHTML;
ip.ondblclick = function () { removeInput(ip); };
td.innerHTML='';
td.appendChild(ip);
td.onmouseup=null;
}
function removeInput(input) {
var val = input.value;
var td = input.parentNode;
td.removeChild(td.lastChild);
td.innerHTML = val;
td.onmouseup = function () { AddInput(td); };
}
function zxcAddField(nn,type,nme){
var obj;
try {
obj=document.createElement('<'+nn+' name="'+(nme||'')+'" '+(type?'type="'+type+'" ':'')+' >');
}
catch(error){
obj=document.createElement(nn);
if (type){
obj.type=type;
}
obj.name=nme||'';
}
return obj;
}
/*]]>*/
</script>
<script type="text/javascript">
/*<![CDATA[*/
function Init(){
INPUT('tst');
}
if (window.addEventListener){
window.addEventListener('load',Init, false);
}
else if (window.attachEvent){
window.attachEvent('onload',Init);
}
/*]]>*/
</script>
</head>
<body>
<a href="admin.php">back</a>
<?php
echo "<table border=1 id='tst' class='tab'>";
echo "<tr><th class='data' rowspan='2' ><strong>EMPLOYEE</strong></th>";
echo "<th class='data' rowspan='2'>DATE</th>
<th colspan='2'>AM</th>
<th colspan='2'>PM</th>
<th colspan='2'>OVERTIME</th></tr>";
echo "<tr>
<th>Arrival</th>
<th>Departure</th>
<th>Arrival</th>
<th>Departure</th>
<th>In</th>
<th>Out</th>
</tr>";
$query="SELECT
employee_detail.employee_code,
employee_detail.lname,
employee_detail.fname,
employee_detail.mname,
employee_record.date,
employee_record.am_in,
employee_record.am_out,
employee_record.pm_in,
employee_record.pm_out,
employee_record.over_in,
employee_record.over_out
FROM employee_detail INNER JOIN employee_record ON employee_record.employee_code=employee_detail.employee_code ORDER BY id DESC ";
$result=mysql_query($query);
$affected=mysql_affected_rows();
while($affected>=1&&$row=mysql_fetch_array($result))
{
echo '<tr><td>';
echo $row['lname'].", ";
echo $row['fname']." ";
echo $row['mname']."</td>";
echo '<td>'.$row['date'].'</td>';
echo '<td><span name="am_in">'.$row['am_in'].'</span></td>';
echo '<td><span name="am_out">'.$row['am_out'].'</span></td>';
echo '<td><span name="pm_in">'.$row['pm_in'].'</span></td>';
echo '<td><span name="pm_out">'.$row['pm_out'].'</span></td>';
echo '<td><span name="over_in">'.$row['over_in'].'</span></td>';
echo '<td><span name="over_out">'.$row['over_out'].'</span></td></tr>';
$affected--;
}?>
<input id="save" type="submit" value="Save Changes" onclick=""/>
</body>
</html>
答案 0 :(得分:2)
我看不懂这个。我已经尝试了15分钟。
要保存到数据库,您需要对服务器进行AJAX调用,在您的情况下,这可能意味着您将为此目的创建另一个PHP脚本,或者将您的脚本划分为多个文件...
但是你最终会在这个“保存按钮”上附加一个点击事件,然后执行以下操作:
var xhr = makeXmlObject();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
alert("saved...hopefully...");
}
}
xhr.open("POST", "myscript.php", true);
xhr.send("name=cheese");
您还需要将标题设置为表单编码,并制作另一个脚本....并制作makeXmlObject函数...但这应该可以使您走上正确的轨道。
或者你可以使用一个库,其中大部分内容都是为你抽象的,所以你的一切都不是那么神秘......这就是我前一段时间所做的,现在不记得如何使用直接的xmlhttprequest对象了...
无论如何,祝你好运。