我正在使用contenteditable功能,我正在尝试更新多个字段,但是当我尝试通过2个字段时,这似乎不起作用,它与一个完美匹配。当我单击“保存”按钮时,我甚至没有收到错误消息。
我相信我没有正确传递数据,这是因为我缺乏知识。
index.php
<?php
//get data from database.
include("db.php");
$sql = mysql_query("select * from datadump where id='1'");
$row = mysql_fetch_array($sql); ?>
<div id="wrap">
<header><h1>Contenteditable Database driven</h1></header>
<div id="status"></div>
<div id="maincontent">
<div id="editable" contentEditable="true">
<?php
echo $row['content'];
?>
</div>
<div id="editablea" contentEditable="true">
<?php
echo $row['name'];
?>
</div>
<button id="save">Save</button>
</div>
JS / js.js
$(document).ready(function() {
$("#save").click(function (e) {
var content = $('#editable').html();
var name = $('editablea').html();
$.ajax({
url: '/save.php',
type: 'POST',
data: {
content: content,
name: editablea
},
success:function (data) {
if (data == '1')
{
$("#status")
.addClass("success")
.html("Data saved successfully")
.fadeIn('fast')
.delay(3000)
.fadeOut('slow');
}
else
{
$("#status")
.addClass("error")
.html("An error occured, the data could not be saved")
.fadeIn('fast')
.delay(3000)
.fadeOut('slow');
}
}
});
});
$("#maincontent").click(function (e) {
$("#save").show();
e.stopPropagation();
});
$(document).click(function() {
$("#save").hide();
});
});
save.php
<?php
include("db.php");
$content = $_POST['content'];
$name = $_POST['editablea'];//get posted data
$content = mysql_real_escape_string($content); //escape string
$sql = "UPDATE datadump SET content = '$content', name = '$name' WHERE id = '1' ";
if (mysql_query($sql))
{
echo 1;
}
?>
答案 0 :(得分:1)
而不是
data: {
content: content,
name: editablea
},
尝试:
data: {
contentLabel: content,
nameLabel: name
},
edittablea不是定义的javascript变量。
$("#save").click(function (e) {
var content = $('#editable').val();
var name = $('#editablea').val();
alert(content);
alert(name);
$.ajax({
url: 'save.php',
type: 'POST',
data: {
contentLabel: content,
nameLabel: name
},
success:function (data) {
//save.php
<?php
$content = $_POST['contentLabel'];
$name = $_POST['nameLabel'];
此外,如果save.php与您的ajax调用脚本位于同一文件夹中,那么您不需要在/save.php前面使用斜杠