SKU 그룹, Group_ID, 편집 단추 및 삭제 단추의 4 개 열이있는 HTML 표가 있습니다. 지금은 삭제 기능에 대한 작업을하고 있으므로 삭제 버튼을 누를 때마다 확인 상자가 팝업되고 "확인"을 누르면 행이 삭제되고 데이터베이스에서 삭제되는 삭제 쿼리가 전송됩니다.단추의 표 행 삭제 Ajax 사용
나는 삭제 쿼리를 위해 Ajax와 별도의 PHP 스크립트를 사용 하겠지만, 알아낼 수는 없다. 어떤 도움을 주셔서 감사합니다! 삭제 버튼에 대한
HTML :
<td><input type="button" class="delete" name="delete" value="Delete" onclick="deleteRow(this)"></td>
자바 스크립트는 ... 나는이 몇 가지 작업을 필요로 알고 있지만 내 질문을 위해 그것을 게시 오전 :
function deleteRow(r) {
if (confirm('Are you sure you want to delete this entry?')) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("skuTable").deleteRow(i);
}
request = $.ajax({
type: "POST",
url: "delete.php",
data: i
});
request.done(function (response, textStatus, jqXHR){
if(JSON.parse(response) == true){
console.log("row deleted");
} else {
console.log("row failed to delete");
}
});
// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// Log the error to the console
console.error(
"The following error occurred: "+
textStatus, errorThrown
);
});
// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function() {
});
}
delete.php :
<?php
$SKU_Group = $_POST['SKU Group'];
$Group_ID = $_POST['Group_ID'];
$host="xxxxxx";
$dbName="xxxxxx";
$dbUser="xxxxxxxxxxxxxx";
$dbPass="xxxxxxxxxxx";
$pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);
$delete = "DELETE FROM SKU_Group_Dim WHERE Group_ID = '$Group_ID'";
$stmt = $pdo->prepare($delete);
$result = $stmt->execute();
echo json_encode($result);
if(!$result) {
echo json_encode(sqlsrv_errors());
}
?>
[Little Bobby] (http://bobby-tables.com/)에서 *** [귀하의 스크립트는 SQL 주입 공격의 위험에 있습니다.] (http://stackoverflow.com/questions/60174/how- can-i-prevent-sql-injection-in-php) ***. 심지어 [이스케이프 문자열] (http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) 안전하지 않습니다! –
옳은 길을 걷고있는 것 같습니다. 작동하지 않는 것은 무엇입니까? –
아약스의 데이터 변수에는 보이는 것에서 $ _POST 게터에 해당하는 매개 변수가 없습니다. –