注意:未定义的索引:第4行的C:\ xampp \ htdocs \ ajax-complete \ save_post.php中的postId 注意:未定义的索引:第8行的C:\ xampp \ htdocs \ ajax-complete \ save_post.php中的postTitle 注意:未定义的索引:第8行的C:\ xampp \ htdocs \ ajax-complete \ save_post.php中的postDescription
<?php
$conn = mysqli_connect("localhost","root","","ajax_complete");
if($_POST["postId"] != ''){
$sql = "UPDATE tbl_post SET post_title ='".$_POST['postTitle']."', post_description = '".$_POST['postDescription']."' WHERE id ='".$_POST['postId']."'" ;
mysqli_query($conn,$sql);
}else{
$sql = "INSERT INTO tbl_post (post_title,post_description,post_status) VALUES('".$_POST['postTitle']."','".$_POST['postDescription']."','draft')";
mysqli_query($conn,$sql);
echo mysqli_insert_id($conn);
}
?>
HTML代码如下:
<h3>Auto save data </h3>
<div class="form-group">
<lable>Post Title</lable>
<input type="text" name="post_title" id="post_title" class="form-control"/>
</div>
<div class="form-group">
<lable>Post Description</lable>
<input type="text" name="post_description" id="post_description" class="form-control"/>
</div>
<div class="form-group">
<input type="hidden" name="post_id" id="post_id" class="form-control"/>
</div>
<div id="autosave"></div>
</div>
<script src="./js/jquery-1.11.2.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
function autosave(){
var post_title = $('#post_title').val();
var post_description = $('#post_description').val();
var post_id = $('#post_id').val();
if(post_title != '' && post_description != '')
{
$.ajax({
url:"save_post.php",
method:"POST",
data:{postTitle:post_title,postDescription:post_description,postId:post_id},
dataType:"text",
success:function(data){
if(data != ''){
$('#post_id').val(data);
}
$('#autosave').text('post save in DB');
setInterval(function(){
$('#autosave').text('');
},2000);
}
});
}
}
setInterval(function(){
autosave();
},10000);
});
</script>
<script>
答案 0 :(得分:0)
在浏览器中加载页面时,代码的结构方式是评估if($_POST["postId"] != '')
;但由于它是一个简单的页面加载,因此没有$_POST
数组,因此没有警告。添加额外的$ _POST检查,以便在正常页面加载期间跳过整个代码块。
if($_POST) {
if($_POST["postId"] != ''){
$sql = "UPDATE tbl_post SET post_title ='".$_POST['postTitle']."', post_description = '".$_POST['postDescription']."' WHERE id ='".$_POST['postId']."'" ;
mysqli_query($conn,$sql);
}else{
$sql = "INSERT INTO tbl_post (post_title,post_description,post_status) VALUES('".$_POST['postTitle']."','".$_POST['postDescription']."','draft')";
mysqli_query($conn,$sql);
echo mysqli_insert_id($conn);
}
}
答案 1 :(得分:-1)
在你的PHP脚本中,你应该使用“post_id”,“post_title和”post_description“。在前端使用Dash(输入标签的name属性),所以你需要在$ POST中使用相同的。