我創建了一個項目計劃器,其工作原理與購物清單非常相似,客戶端一切正常;但是,當我嘗試將新的附加元素添加到數據庫時,它不起作用。一切都會被添加到數據庫中,如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);
?>
@弗雷德-II-thxs但是這不是我的問題,問題是,PHP是說,ELEM變量是空的 – swsa
我刪除了我的評論,意識到這不是你需要的。 –