Reputation: 45
I have a one form and which have multiple form fields with remove button.All data is coming from DB.now when i am click on remove button i want to remove this data from DB using ajax.
PHP Code:-
<?php
for($i=0;$i<$hdntotal;$i++)
{
?>
<div class="expform" id="<?=$i;?>">
<label class="explabel">Expert Name: </label><input type="text" id="txtexpname" name="txtexpname[]" class="expfield" placeholder="Enter name of expert" value="<?=$expert_name[$i]; ?>"/>
<div class="clearboth"></div>
<label class="explabel">Link of the expert work: </label><input type="text" id="txtexplink" name="txtexplink[]" class="expfield" placeholder="Enter link of expert work" value="<?=$expert_user_links[$i];?>"/>
<div class="clearboth"></div>
<label class="explabel">Title of the review: </label><input type="text" id="txtreviewtitle" name="txtreviewtitle[]" class="expfield" placeholder="Enter title of review" value="<?=$expert_title[$i];?>"/>
<div class="clearboth"></div>
<label class="explabel">Details of the review: </label>
<textarea id="txtrevdetails" name="txtrevdetails[]" class="expfield" placeholder="Enter details of review"><?=$expert_details[$i]; ?></textarea>
<div class="clearboth"></div>
<input type="hidden" value="<?=$rev_id[$i];?>" name="oldexprevids[]" >
<input type="button" class="delReview" id="<?=$rev_id[$i];?>" value="Remove">
<div class="line"></div>
</div>
<?php
}
?>
JS Code:-
/*AJAX Function for Delete Expert Review*/
jQuery(document).ready(function($) {
$('button.delReview').each(function(){
var $this = $(this);
$this.click(function(){
var deleteItem = $this.attr('id');
$.ajax({url:'delete-expreview.php?action='+deleteItem}).done(function(data){
//colect data from response or custom code when success
});
return false;
});
});
});
/*End fo AJAX Function*/
Delete Page Code:-
$id = $_REQUEST['action'];
$sql5 = "delete from cc_tbl_car_review where id = '$id'";
$result5 = mysqli_query($db,$sql5) or die('Fetch Error');
But i am not getting any result.if you have any solution then please share it with me.
Upvotes: 0
Views: 2844
Reputation: 2962
Pass Unique Id to ajax function . Onclick function should get unique Id for deletion. and Ajax Code Would be like this
$('.delReview').click(function(){
var id =$(this).attr('id');
$.ajax({
type: "POST",
//path to delete php page
url:"pathto/delete.php",
data: "id="+id,
success:function(result){
//here is your success action
//for refreshing page use this
$("#result1").html(result);
});
});
And in Your delete.php get unique id like $_POST['id'] and perform action.
Upvotes: 2
Reputation: 68
The Problem is with your ajax call. we have to pass data in name: value paai like
your ajax call will be look like this
$.ajax({
url:'delete-expreview.php',
data:{action:deleteItem},
success:function(result){alert("message");}
});
Upvotes: 0