Click here to Skip to main content
15,881,725 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I'm trying to delete a specific ID from my questions list. Right now it's only deleting the last question from the list of questions not the one I click on to delete.

HTML
HTML
<div id="wrapper">
        <div class="projects_list">



         </div>
      </div>



MY JQUERY
 var projects = function(){

     $.ajax({
         url: "xhr/get_projects.php",
         type: "get",
         dataType: "json",
         success: function(response){
             if(response.error){
                 console.log(response.error);
             }else{


                 for(var i=0, j=response.projects.length; i < j; i++){
                     var result = response.projects[i];
                     console.log(response.projects[i]);

                     $(".projects_list").append(
                        "<div style='border: 1px solid #555'>" +
                         "<h5> Question ID:</h5> " + result.id + "<br>" +
                         " Question: " + result.projectName + "<br>" +
                         " Question tags: " + result.projectDescription + "<br>" +
                         " <button class='deletebtn'>Delete</button>" +
                      //   " Question Status: " +result.status +
                     //  "<button class='editbtn'>Edit</button>" +
                         "</div> <br>"
                    );

                 };
             $(".deletebtn").on("click", function(e){
                console.log("test delete");
                 $.ajax({
                     url: "xhr/delete_project.php",
                     data: {
                         projectID: result.id
                     },
                     type: "post",
                     dataType: "json",
                     success: function(response){
                         console.log("testing for success");

                         if(response.error) {
                             alert(response.error);

                         }else{
                             window.location.assign("questions.html");
                         };
                     }
                 });
             });
          };
         }
      });
};

MY PHP FOR DELETE_PROJECT.PHP

PHP
<?php

// UNFINISHED

error_reporting(E_ALL);

session_start();
session_regenerate_id(false);

require_once("reqs/common.php");
require_once("reqs/pdo.php");
require_once("reqs/auth.php");

checkLoggedIn();

$updata = array();

$user = $_SESSION["user"];
$projectID = param($_POST, 'projectID', '');

if( empty($projectID) ){ errormsg("The 'projectID' is required."); }

$dbh = new PDB();
$db = $dbh->db;
$site = new Site($db);

$tasks = $dbh->getTasks($projectID);
$taskArr = array();

for($i=0; $i<count($tasks); $i++){
	array_push($taskArr, $tasks[$i]->id);
}

$arr = implode(", ", $taskArr);

try{
	$ct = 0;
	if(!empty($arr)){ //rich and justin edit
		$sql = "DELETE FROM tasks WHERE id IN (".$arr.")";
		//errormsg($arr);
		$st = $db->prepare($sql);
	
		$st->execute();
	}
}catch (PDOException $e){
	errormsg($e->getMessage());
}

try{
	$ct = 0;
	$sql = "DELETE FROM projects WHERE id = :projectID";
	
	$st = $db->prepare($sql);
	$st->bindParam(":projectID", $projectID);
	
	$st->execute();
	
}catch (PDOException $e){
	errormsg($e->getMessage());
}

exitjson(array("success"=>true));


?>



Thank you in advance.
Posted
Updated 21-May-14 19:11pm
v4

Hello jgoldd,

Please update your code as following:
JavaScript
$(".projects_list").append(
  "<div style="border: 1px solid #555">" +
  "<h5> Question ID:</h5> " + result.id + "<br>" +
  " Question: " + result.projectName + "<br>" +
  " Question tags: " + result.projectDescription + "<br>" + 
  " <button resultID='"+ result.id +"' class='deletebtn'>Delete</button>" +
  "</br></br></br></div> <br>");

$(".deletebtn").on("click", function(e){
   console.log("test delete");
   var resultID= $(this).attr(resultID);
   $.ajax({
       url: "xhr/delete_project.php",
       data: {
           projectID: resultID
       },
       type: "post",
       dataType: "json",
       success: function(response){
           console.log("testing for success");                            
           if(response.error) {
               alert(response.error);
           }else{
               window.location.assign("questions.html");
           };
       }
   });
});</br>
 
Share this answer
 
Thank you, that worked but for some reason I had to remove the ".val()" because it kept giving me an error of "undefined is not a function".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900