我有一个将我的数据插入我的数据库的功能。但同时我希望我的javascript将内容添加到我的列表#div中。
我的php文件
function xhrInsertPost() {
// echo $_POST[''];
$title = $_POST['title'];
$msg = $_POST['msg'];
$sth = $this->db->prepare('INSERT INTO posts (post_title, post_content) VALUES (:title, :msg) ');
$sth->execute(array(
':title' => $title,
':msg' => $msg
));
$data = array( 'post_title' => $title, 'id' => $this->db->lastInsertId());
echo json_encode($data);
}
我的js文件
$('#postText').submit(function() {
var url = $(this).attr('action');
var data = $(this).serialize();
$.post(url, data, function(data) {
console.log(data);
console.log(data.msg);
$('#listMsg').append('<tr><td>' + data.post_title + '</td><td><a class="del" rel="'+ data.post_id + '" href="#"> delete</a></td></tr>');
}, 'json');
return false;
});
我认为它不起作用,因为我的专栏名称不同。我的代码工作,但我的“data.post_id”是未定义..在我的其他方法(如果我刷新页面)它工作