我正在使用主页,并将为管理员使用AJAX内联编辑脚本,使其尽可能简单。 我一直在使用的脚本是this,它几乎包含了我想要的内联编辑脚本。当我要捕获新的更改并将它们发送到PHP函数时,我的问题就出现了,它将使用这些新的更改来更新我的数据库。
我没有那么多AJAX和PHP的经验,所以我有点迷茫,但我尝试了一个代码,我找到了:
$.ajax({
type: "POST",
url: "update_handler.php",
data: "newfieldvalue=" + newValue,
success: function(msg){
alert( "Data Saved: " + msg );
}
});
问题在于我不太清楚如何或在何处实现此代码,或者它甚至是使用正确的代码。为了向您展示我附加了两个txt文档的代码:
和
在index.php.txt
中是索引页面,它从数据库中检索我的数据并使用一些jQuery代码。在jQuery.editableText.js.txt
中是具体的jQuery代码。我想PHP处理程序页面非常标准,可以使用正确的字段然后在数据库中更新它。
答案 0 :(得分:1)
我对你有疑问:
$ menuID包含某些内容的ID,您可以使用它通过此ID从表中获取它。这是对的吗?
如果正确,您必须将此ID传递给PHP处理程序页面。
示例:
的index.php:
<script type="text/javascript">
jQuery(function($){
$('h2.editableText, p.editableText').editableText({
newlinesEnabled: false
});
$.editableText.defaults.newlinesEnabled = true;
$('div.editableText').editableText();
$('.editableText').change(function(){
var newValue = $(this).html();
// important code:
$.ajax({
type: "POST",
url: "save.php",
data: { val : newValue, key:$(this).parent().tagName, id:$(this).parent().attr('class')},
success: function(msg){
alert( "Data Saved: " + msg );
}
});
});
});
</script>
和身体部位:
<body>
<div id="wrapper">
<div id="content">
<?php
$isnull = getContent($menuID, "title");
if ($isnull != "") {
echo "<h2 class=\"editableText\"><center><p>" . getContent($menuID, "title") . "</p></center></h2><br>";
} else {
null;
}
?>
<div class="editableText">
<p class="<?php echo $menuID?>"><?php echo getContent($menuID, "maincontent");?></p>
</div>
</script>
<?php
mysql_close($connection);
?>
还有一个, save.php:
<?php
# content that you send from client; you must save to maincontent
$content=$_POST['val'];
# $from=='div' if it from maincontent or $from=='center' if it from title
$from=$_POST['key'];
# id of your post
$id=$_POST['id'];
#here you can save your content;
?>
答案 1 :(得分:0)
正如edit-in-page page所说,你应该在脚本块中使用该代码。所以你几乎拥有它。以下内容应该有效(未经测试)。
<script type="text/javascript">
jQuery(function($){
$('h2.editableText, p.editableText').editableText({
newlinesEnabled: false
});
$.editableText.defaults.newlinesEnabled = true;
$('div.editableText').editableText();
// bind an event listener that will be called when
// user saves changed content
$('.editableText').change(function(){
var newValue = $(this).html();
// do something
// For example, you could place an AJAX call here:
$.ajax({
type: "POST",
url: "update_handler.php",
data: "newfieldvalue=" + newValue,
success: function(msg){
alert( "Data Saved: " + msg );
}
});
});
});
</script>