这是在此处构建的示例,除了 php 代码: http: //jsfiddle.net/MJU8S/ 每当编辑字段时,如果按下回车,则应将更新发送到数据库。问题是根本没有发送任何内容。
我该如何解决这个烂摊子?甚至可能吗?
<script type="text/javascript">
function edit(){
var input = this.getElementsByTagName("input")[0];
input.removeAttribute("disabled");
input.focus();
input.addEventListener("keypress", save, false);
}
function save(e){
if (e.type == "keypress" && e.keyCode != 13)
return;
this.setAttribute("disabled", "disabled");
/*removed*/document.cookie = "variable=" + this.getAttribute("name");
/*removed*/document.cookie = "value=" + this.getAttribute("value");
/*removed*/document.cookie = "id=" + this.getAttribute("id");
/*removed*/
<?php
$variable = $_COOKIE["variable"];
$value = $_COOKIE["value"];
$id = $_COOKIE["id"];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<script type=\"text/javascript\">parent.alert('Errore nella connessione al database: salvataggio interrotto.');</script>");
$update_query = "UPDATE Serie SET `$variable`='$value' WHERE `id`='$id'";
mysql_query($update_query) or die("<script type=\"text/javascript\">parent.alert('Errore nella query: salvataggio interrotto.');</script>");
mysql_close();
?>
}
</script>
你对声望点很严厉,不是吗?=P 顺便说一句...
编辑:感谢您的课程,这是我第一次为网络做点什么。如果我理解正确,我需要 Ajax 做一些不刷新的事情,像这样?
<script type="text/javascript">
function edit(){
var input = this.getElementsByTagName("input")[0];
input.removeAttribute("disabled");
input.focus();
input.addEventListener("keypress", save, false);
}
function save(e){
if (e.type == "keypress" && e.keyCode != 13)
return;
this.setAttribute("disabled", "disabled");
/*new*/var value = this.getAttribute("value");
/*new*/if(value == "" || value == null)
/*new*/ return;
/*new*/ var variable = this.getAttribute("name");
/*new*/ var id = this.getAttribute("id");
/*new*/ xmlhttp.open("POST","update_query.php",true);
/*new*/ xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
/*new*/ xmlhttp.send("variable="+variable+"&value="+value+"&id="+id);
}
</script>
然后我想把这个连接放在“update_query.php”中:
<?php
$variable = $_POST["variable"];
$value = $_POST["value"];
$id = $_POST["id"];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<script type=\"text/javascript\">parent.alert('Errore nella connessione al database: salvataggio interrotto.');</script>");
$update_query = "UPDATE Serie SET $variable='$value' WHERE id='$id'";
mysql_query($update_query) or die("<script type=\"text/javascript\">parent.alert('Errore nella query: salvataggio interrotto.');</script>");
mysql_close();
?>
编辑:我得到了一些错误的价值。
function save(e){
if (e.type == "keypress" && e.keyCode != 13)
return;
this.setAttribute("disabled", "disabled");
/*new*/var value = e.target.value;
if(value == "" || value == null)
return;
/*new*/ var variable = e.target.name;
/*new*/ var id = e.target.id;
xmlhttp.open("POST","update_query.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("variable="+variable+"&value="+value+"&id="+id);
}