teensicle Posted January 28, 2013 Share Posted January 28, 2013 (edited) Want to have the user select one or more checkboxes and then click a button to delete all the selected items. Doesn't seem to b working though <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>HTML/JS:</head><body><form id="#your_form"> Check Box: <input type="checkbox" name="check[]" value="1"> Check Box: <input type="checkbox" name="check[]" value="2"> Check Box: <input type="checkbox" name="check[]" value="3"> Check Box: <input type="checkbox" name="check[]" value="4"> <input type="image" src="http://blogs.learnnowonline.com/Portals/153597/images/jquery1.png" width="64" height="64"></form><script>$(document).ready(function(){ $("#your_form").click(function(){ $.get("sql_delete.php",$("#your_form").serialize(),function(response){ alert(response); }); });});</script></body></html> <?phprequire 'functions/init.php'; $messageid = $_POST['check']; foreach($messageid as $id) { $sql_statement = "DELETE FROM chj_messages WHERE message_ID = $id"; mysql_query($sql_statement); }?> Edited January 28, 2013 by teensicle Link to comment Share on other sites More sharing options...
dsonesuk Posted January 28, 2013 Share Posted January 28, 2013 you are sending a get ($.get) but referencing a post $_POST['check']; Link to comment Share on other sites More sharing options...
xhtmlchamps1 Posted January 28, 2013 Share Posted January 28, 2013 Hello friend,you are using $.get method then you have to check the condition $_GET[' '] in php file or else if you are using $.post method you have to check the condition $_POST[' '] in php file. Link to comment Share on other sites More sharing options...
teensicle Posted January 28, 2013 Author Share Posted January 28, 2013 Okay so i tried the code and the execution works and i can positively see that php script request was made. However i am getting an "Undefined error" for the post variable "check" i.e $_POST["check"] and an invalid argument error for the "foreach" array assignment loop which i suspect happened because the post data was not in it's correct form. Any suggestions ? <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script><script>$(document).ready(function(){ $("#img").click(function(){ var chkbx = $("#your_form").serialize(); $.post("sql_delete.php",chkbx,function(data){ alert(data); }); });});</script></head><body><form id="#your_form"> Check Box: <input type="checkbox" name="check[]" value="1"><br> Check Box: <input type="checkbox" name="check[]" value="2"><br> Check Box: <input type="checkbox" name="check[]" value="3"><br> Check Box: <input type="checkbox" name="check[]" value="4"><br></form><img id = "img" src="http://blogs.learnnowonline.com/Portals/153597/images/jquery1.png" width="64" height="64"></body></html> <?php //Look out for SQL-injections $message_id = mysql_real_escape_string($_POST["check"]); if(isset($message_id)){ foreach($message_id as $id){ $query = mysql_query("DELETE FROM `chj_messages` WHERE `message_ID` = '$id'")or die(mysql_error()); } } ?> Link to comment Share on other sites More sharing options...
dsonesuk Posted January 28, 2013 Share Posted January 28, 2013 You are using mysql_real_escape_string() against a array of $_POST["check"], try checking it against each value within the POST array <?php //Look out for SQL-injections $message_id = $_POST["check"]; if(isset($message_id)){ foreach($message_id as $id){$id = mysql_real_escape_string($id); $query = mysql_query("DELETE FROM `chj_messages` WHERE `message_ID` = '$id'")or die(mysql_error()); } } ?> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now