...

View Full Version : jQuery Passing data via Jquery to delete data from a mysql database



ridgey28
12-02-2010, 01:41 PM
Hi I am trying to get data from selected checkboxes using jQuery $post and then getting the values using PHP to delete from a database. I also would like a confirmation dialog to see if the user wants to delete the events. I have looked at various methods on the web but I keep getting stuck. I know a bit about jQuery but not a lot about ajax. Any pointers will be appreciated.

list.php

<form name='delete_form' action='' method='post'>
<input type="checkbox" value="131" name="deleteCB[]" class="cb" />
<input type="checkbox" value="32" name="deleteCB[]" class="cb" />
<input type="checkbox" value="129" name="deleteCB[]" class="cb" />
<input type='submit' id='deleteBtn' value='Delete' name='DeleteBtn' />
</form>

lit.js Not sure if this is correct

$(document).ready(function(){
$("#deleteBtn").click(function() {
var confirmation = confirm("Are You Sure You Want to Delete These Events?")
if(confirmation == true)
{
$(':checkbox:checked').each(function()
{
$.post('process.php', { deleteCB: $(this).attr('value') },
function(msg){$('.result').html(msg);
alert('Items were successfully deleted.')});
});

}
else
{
return false;
}
});
});

process.php

$value=$_POST['deleteCB'];


$db->connect();
$query_delete = "DELETE FROM events WHERE id='$value'";
$db->query($query_delete);
$db->close();



Thanks in advance

Spudhead
12-02-2010, 05:26 PM
You might find this page (http://jetlogs.org/2007/06/17/jquery-tutorial-passing-input-arrays/) useful.

Also: validate your PHP input. What if some malicious person POSTed "' OR 1=1--" ?

ridgey28
12-03-2010, 07:43 PM
Thanks that site gave me some good pointers. I have included validation in my script I just didn't add it to my thread.
:thumbsup:

CoolAsCarlito
12-05-2010, 04:18 PM
I have a similiar use for this however my situation is that I have a listing of entries with checkboxes with their IDs attached to them in the coding but I have a dropdown with edit and delete and then a submit button next to it what kind of change would i make bases on the user's choice.

Both of these code blocks are from the same page.


<div class="listActions">
<form action="" method="post">
<label for="actionSelect">With selected items: </label>
<select class="select" name="actionSelect" id="actionSelect">
<option id="1">Edit</option>
<option id="2">Delete</option>
</select>
<button class="button small-button"><strong>Apply</strong></button>
</form>
</div>




<?php
while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
echo '
<tr>
<td><input type=checkbox class=checkbox value="' . $row['id'] . '" /></td>
<td>' . $row['divisionname'] . '</td>
<td>' . $row['name'] . '</td>
<td class=last>' . $row['datecreated'] . '</td>
</tr>';
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum