Jump to content
teensicle

Execute PHP file with JQUERY when image is clicked

Recommended Posts

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 by teensicle

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 bigsmile.gif?

<!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());	    }    }   ?>

Share this post


Link to post
Share on other sites

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());            }    }   ?>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...