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

3
  • 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? Commented Jan 28, 2013 at 0: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 Commented Jan 28, 2013 at 0: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 Commented Jan 28, 2013 at 0:29

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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
You need to serialize the form values, check this: api.jquery.com/serialize Then you can pass all the IDs as a single value.
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. (api.jquery.com/serialize)
LOL Shomz. beat me to it :)
@thaJeztah Haha, yeah... Though I've mentioned all of it in my answer. :)
|
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);

5 Comments

And what they said about sql injection
it executed the javascript code however the values still exist in the tables
actually, you need a return false; after the post or it will submit the form without the javascript. Check your network headers/response in chrome or with firebug.
I check the network headers in chrome, after the image was clicked it called the js file, the button and the html page however i saw no call to the actual php file that does the deleting
should be <form id="your_form"> without the #, sorry. I edited above this and the return false; mentioned in previous comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.