我正在创建一个项目规划器,其工作方式与购物清单非常相似,在客户端一切正常;但是,当我尝试将新的附加元素添加到数据库时,它不起作用。其他所有内容都将添加到数据库中,如 id 和日期,但不是创建的新元素,我将其保存为文本。php 给我的错误是 $_POST[elem] 没有价值。
jQuery/Ajax
$(document).ready(function(){
// GLOABL VARIABLES
var project = "";
var div = "";
var categoryDiv = "";
var category = "";
// Adding a project
$('.project-btn').click(function(e){
e.preventDefault();
//grab the user input for the new project
var project = $('.project-val').val();
//Add the project to the list
$('<li></li>').addClass(project).appendTo('.project-list');
$('<a></a>').attr("href",project).text(project).appendTo('li.'+project);
// create the div where the categories will go
$('<div></div>').attr("id",project).appendTo('.category-wrapper');
// hide the div untill it is called upon
$('div#'+project).fadeOut(function(){
$('<h1></h1>').text(project).css("text-align","center").appendTo('div#'+project);
// add the input for the category div
$('<input>').attr("type","text").addClass('category-val').appendTo('div#'+project);
$('<input>').attr("type","submit").attr("value","Add Category").addClass("category-btn").appendTo('div#'+project);
// add the back button
$('<p></p>').text("back").addClass('category-back').css("cursor","pointer").appendTo('div#'+project);
// add the ul
$('<ul></ul>').attr("class","category-list").appendTo('div#'+project);
});
// clear the input search
$('.project-val').val('');
// add to the database
var elem={};
elem ='<li class="'+project+'">'+project+'</li>';
$.ajax({
url: 'project_list.php',
data: elem,
type: 'POST',
success: function(response)
{
alert("success");
}
});//end of ajax call
});
});
PHP:
<?php
include 'inc/connect.php';
$string = $_POST['elem'];
mysqli_query($con, "INSERT INTO `project_list` (string) VALUES ('$string')");
mysqli_close($con);
?>