I want to create a program where I can delete mysql records using button with ajax. My problem is that I don't know how to pass a mysql data as ajax parameter so if it matches with the query in the php file, it will be removed. I created one with php alone but it's a static program, I want it to be dynamic with ajax.
index.php :
<?php
require_once 'dbconfig.php';
$query = $con->query("SELECT * FROM statuspost ORDER BY id DESC");
while($i = $query->fetch_object()){
echo $i->post ?><button onclick='ajaxWallpost('.<?php
$i->id ?>.')'>Remove</button>
<?php
}
?>
<script src='https://ajax.googleapis.com/ajax/libs/prototype
/1.7.2.0/prototype.js'></script>
<script>
function ajaxWallpost(status){
new Ajax.Request('wallpost.php?type=post&
id='+status, {
method:'get',
onSuccess: function(transport) {
},
onFailure: function() { alert('Something went
wrong...'); }
});
}
</script>
wallpost.php :
<?php
require_once 'dbconfig.php';
if(isset($_GET['id'], $_GET['type'])){
$post = $_GET['type'];
$id = (int)$_GET['id'];
switch($post){
case 'post':
$con->query("DELETE FROM statuspost WHERE id={$id}");
break;
}
}
Any help will be greatly appreciated. Thanks