1

hope everyone is enjoying their day coding so far :D. i'm relatively new to JQUERY and AJAX been trying to find my way around, i'm getting there though. I have one minor problem however. I have HTML content that was generated from a MYSQL database using PHP (so the values for the checkboxes vary); it's a simple internal messaging program that i am working on for my website. I have a single check box that i can click that will select all the other checkboxes on the page. However what i hope to achieve is when ever the user selects a specific amount of check boxes or even a few, then presses a picture it with then call on my php file which will be responsible for deleting the message which the user checked. This previous question helped alot: How to pass jQuery variables to PHP variable? but i have numerous check boxes

HTML/JS:

<body>
    Check Box: <input type="checkbox" name="check" value="1">
Check Box: <input type="checkbox" name="check" value="4">
Check Box: <input type="checkbox" name="check" value="3">
Check Box: <input type="checkbox" name="check" value="4">

<img id = "delete" src="http://icons.iconarchive.com/icons/visualpharm/must-have/256/Delete-icon.png">

<script>
$("#delete").click(function () {
   $.post('sql_delete.php', 'num=' + $(this).val(), function (response) {
      alert(response);
   });
}); 
</script>
</body>

PHP

<?php
    require 'functions/init.php';

        $messageid = "_$POST[num]";

        $sql_statement = "DELETE FROM chj_messages WHERE message_ID = $messageid";
        mysql_query($sql_statement);


?>

I suspect that i might need loops in both the JS and PHP not entirely sure though. All your beautiful suggestions are welcomed :D

Community
  • 1
  • 1
Darien Ruddock
  • 29
  • 3
  • 10
  • 1
    Do you want to delete an item at the moment a checkbox is clicked, or have the user select one or more checkboxes and then click a button to delete all the selected items? – thaJeztah Jan 28 '13 at 00:17
  • Also, your code is open for SQL-injection to prevent this, either properly escape the $messageid before using it in a SQL statement, or try to use parameter binding for your SQL – thaJeztah Jan 28 '13 at 00:19
  • Yes thaJeztah that is exactly what i hope to achieve :D... the code i used was just a quick mark up, the original code protects against sql injection.. Well i hope it does a good job at doing that :D – Darien Ruddock Jan 28 '13 at 00:29

2 Answers2

0

$messageid = "_$POST[num]"; should be $messageid = $_POST['num'];

Your code will delete one by one checkbox, as the user clicks them. If you want to let users check as many as they want, and then click some button to delete them, you need to remove that click listener from the checkboxes and bind it to a submit button or the image you mentioned. You'll also need to change the way you make your ajax call - you'll have to serialize all the checked ids and send that. Also, the PHP code needs to unserialize that data and make a DELETE query for each received ID.

Also, probably the most important, read about SQL Injection before going live with any site that uses MySQL. And mysql_* functions are deprecated, like it says on php.net, so you better switch to mysqli_* or PDO.

Shomz
  • 37,421
  • 4
  • 57
  • 85
  • Hey shomz thanks for your quick response i fixed that. I was wondering how then can i pass multiple variables of the check box values to the php code when ever i click that picture. Thanks :D – Darien Ruddock Jan 28 '13 at 00:21
  • You need to serialize the form values, check this: http://api.jquery.com/serialize/ Then you can pass all the IDs as a single value. – Shomz Jan 28 '13 at 00:22
  • 1
    you'll have to wrap them in a form and add a 'submit' button probably. Using jQuery.serialize() you'll probably be able to collect all form data at once. (http://api.jquery.com/serialize/) – thaJeztah Jan 28 '13 at 00:23
  • LOL Shomz. beat me to it :) – thaJeztah Jan 28 '13 at 00:24
  • @thaJeztah Haha, yeah... Though I've mentioned all of it in my answer. :) – Shomz Jan 28 '13 at 00:25
  • Agreed, we won't hear from him in a while! :D – Shomz Jan 28 '13 at 00:27
  • Hey thanks Shomz and thaJeztah i'll get cracking on the reading. The code i am using for my php isn't actually the real code i am only using it as a easy solution for testing if it works. Thanks for all the suggestions i will let you know how it goes – Darien Ruddock Jan 28 '13 at 00:32
0

jquery:

$('#your_form').submit(function() {
    $.post('sql_delete.php', $("#your_form").serialize(), function (response) {
        alert(response);
    });
    return false;
});

html:

<form id="your_form">
  Check Box: <input type="checkbox" name="check[]" value="1">
  Check Box: <input type="checkbox" name="check[]" value="6">
  Check Box: <input type="checkbox" name="check[]" value="8">
  Check Box: <input type="checkbox" name="check[]" value="4">
  <input type="image" src="path/to/your/image">
</form>

php:

$messageid = $_POST['check'];
foreach($messageid as $id) {
     $sql_statement = "DELETE FROM chj_messages WHERE message_ID = $id";
      mysql_query($sql_statement);

}

the edited code above has been tested.

also, the php should probably be done without a loop

$messageid = $_POST['check']; // sql injection already discussed.
$in = implode(", ", $messageid);
$sql_statement = "DELETE FROM chj_messages WHERE message_ID IN ($in)";
mysql_query($sql_statement);