0

I'm creating a project planner where that works very similar to a shopping list everything is working fine on the client side; however, when I try to add the new appended element to the database it doesn't work. everything else gets added to the database as in the id and the date but not the new element created which I'm saving it as text. The error that php is giving me is that $_POST[elem] has no value.

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);

?>
swsa
  • 225
  • 1
  • 4
  • 13

1 Answers1

2

The data would have to be '"elem=" + elem', here is an example.

Your code also is vulnerable to SQL Injections, check this out to prevent them: How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
1n9i9c7om
  • 93
  • 8