I am that geek who is working in the programming field, in love with poetry, always appreciate good lyrics and addicted to loud music.
PHP + MySQL + jQuery: Deleting multiple selections
A user from a well known local forum requested for help on how can he (or she) able to allow the system to delete multiple selections, including “select all” as we always see at our emails’ inbox.
So I decided to show my way of making it which uses PHP, MySQL and jQuery. I chose jQuery because it makes your codes look neat without any “interference” from Javascript. Javascript for multiple selections using jQuery was adapted from a tutorial at jetlogs. Few modifications were made to suit the purpose of this tutorial.
File: select_all.js
-
-
$(document).ready(function()
-
{
-
$("#select_all").click(function()
-
{
-
var checked_status = this.checked;
-
$("input[@name='messages[]‘]").each(function()
-
{
-
this.checked = checked_status;
-
});
-
});
-
});
Explanation: select_all.jsp is to allow users to select all messages available in their inbox with one click. By using jQuery as its frame work, the possibility for this script to work on most browser is high.
File: index.php
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
-
<html>
-
<head>
-
<title>PHP + MySQL + jQuery Demo | Deleting Multiple Messages</title>
-
<script type="text/javascript" src="jquery.js"></script>
-
<script type="text/javascript" src="select_all.js"></script>
-
</head>
-
-
<body>
-
<form method="post" action="delete.php">
-
<?php
-
//connecting to MySQL and select Database
-
?>
-
<table border="2">
-
<tr>
-
<td align="center"><input type="checkbox" id="select_all"></td>
-
<td align="center">Subject</td>
-
</tr>
-
<?php
-
-
$sql = "SELECT COUNT(*) FROM delete_multiple_messages WHERE ip_address = ‘".$_SERVER[‘REMOTE_ADDR’]."’";
-
-
{
-
//Generate number of messages for example
-
$count = 1;
-
-
while($count < $totalMessages)
-
{
-
$sql = "INSERT INTO delete_multiple_messages VALUES(”,’message number ".$count."’, ‘".$_SERVER[‘REMOTE_ADDR’]."’)";
-
$count++;
-
}
-
}
-
-
$sql = "SELECT * FROM delete_multiple_messages WHERE ip_address = ‘".$_SERVER[‘REMOTE_ADDR’]."’";
-
-
{
-
?>
-
<tr>
-
<td align="center"><input type="checkbox" name="messages[]" value="<?php echo $result['message_id']; ?>"></td>
-
</tr>
-
<?php
-
}
-
-
?>
-
<tr>
-
<td colspan="2" align="left"><input type="submit" name="submit" value="Submit"></td>
-
</tr>
-
</table>
-
<p>
-
<a href="http://validator.w3.org/check?uri=referer">
-
<img src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01 Strict" height="31" width="88" style="border:0">
-
</a>
-
</p>
-
</form>
-
</body>
-
</html>
Explanation: In this script both jQuery and select_all javascripts are being imported in the header section. I did not display it here, with assumption that you have javascript and html backgrounds. PHP function commences by connecting to server and selecting related database.
File: delete.php
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
-
<html>
-
<head>
-
<title>PHP + MySQL + jQuery Demo | Deleting Multiple Messages</title>
-
</head>
-
-
<body>
-
<?php
-
-
{
-
$count = 0;
-
foreach($_POST[‘messages’] as $current)
-
{
-
$deleteMessages = ($count < 1) ? " message_id = ‘".$current."’" : $deleteMessages." OR message_id = ‘".$current."’";
-
$message .= $current." ";
-
-
$count++;
-
}
-
-
$sql = "DELETE FROM delete_multiple_messages WHERE ".$deleteMessages." AND ip_address = ‘".$_SERVER[‘REMOTE_ADDR’]."’";
-
-
{
-
?>
-
<?php
-
}
-
}
-
-
?>
-
</body>
-
</html>
Explanation: Once user have clicked submit button, system will check which messages being selected. Using foreach function, related messages’ id are stored which soon will be deleted from database. Once all selected messages is stored, system will proceed with removing these messages from the database.
Working example is available. Remember to include import jQuery and select_all javascript in the header section. If my explanations are not so clear, feel free to tell me.
Possibly related posts (generated by Yet Another Related Posts Plugin):
- [PHP + MySQL + Javascript]: Dynamic AJAX Form
- [PHP + jQuery ThickBox]: AJAX Login
- [PHP + jQuery + ACD]: Checks For Server Status
- PHP + MySQL + cURL: Single Sign On With Multiple Domains
- [PHP 5 + jQuery]: JSON Cross Domain




xphm!!!!!
mmg best tutorial nih