我的问题是update.php只获取发布的表单数据(post_edit
)。通过AJAX早些时候发布的变量不经过如何将AJAX调用与通过表单发布数据到同一文件的php文件同步?
Notice: Undefined index: id_to_edit in ...\update.php on line 5
Notice: Undefined index: column_to_edit in ...\update.php on line 6
我想要做的事:
我有痕迹鼠标放在桌子上的身体位置的回调函数。这样做是为了检测用户想要编辑的单元格的列和id - id整数和列字符串通过AJAX发布到一个php文件中,并在SQL查询中使用这两个值(对于坐标)在顶部用户想要更新的数据(通过表单发布,稍后更多)。
编辑按照这种方式完成:当用户将鼠标放在单元格上时,表单将在内部创建,并填充该表单应该发布数据以更新SQL表中的对应条目(通过使用坐标回调函数)。移出将删除表单。
套用有点
如何发布坐标和表单数据的PHP文件,使所有这些值可以在SQL查询中使用?如果我一直在做的是从根本上打破了,那么还有另一种方法吗?
$(function(){
$("td")
.hoverIntent(
function(e){
var id_of_edit = $(e.target).siblings('th').text();
var $clicked_column_i = $(e.target).index() + 1;
var column_of_edit = $("#tableheader").children("th:nth-child(" + $clicked_column_i + ")").text();
$.ajax({
type: 'POST',
dataType: 'text',
url: 'update.php',
data: {
'id_of_edit': id_of_edit,
'column_of_edit': column_of_edit
},
});
var $edit_button = $('<form action="update.php" method="post"><input type="text" name="post_edit"/></form>');
$(e.target).append($edit_button);
console.log(e.target.innerText + " was clicked");
console.log(id_of_edit + " is the ID");
console.log(column_of_edit + " is the column name");
//just to check the tracer function is working correctly
},
function(e){
$id_of_edit = $(e.target).siblings('th').text();
$clicked_column_i = $(e.target).index() + 1;
$column_of_edit = $("#tableheader").children("th:nth-child(" + $clicked_column_i + ")").text();
$(e.target).children('form').remove();
});
});
update.php:
<?php
include 'config.php';
echo $_POST['id_to_edit'];
echo $_POST['column_to_edit'];
echo $_POST['post_edit'];
$stmt = $pdo->prepare('UPDATE food SET :column = :edit WHERE id = :id');
$stmt->execute([
'id' => $_POST['id_to_edit'],
'column' => $_POST['column_to_edit'],
'edit' => $_POST['post_edit']
]);
?>
代码中也有拼写错误:** _编辑的id _ **与id _ **到** _edit(其他变量对中的同一个拼写错误) – Shadow