When you click on the picture you need it to go away. I attach the code below. Tell me where the error is. When I click on the picture, the page hangs on me, through the debugger it doesn’t look everything hangs tight.

$mnogo = "SELECT * FROM images where tovars_id=$_REQUEST[id]"; $rows=$dbh->query($mnogo)->fetchAll(); <? foreach($rows as $k=>$img){ ?> <img id="output_<?=$k?>" data-id="<?=$img[id]?>" class="modal_images" style="height: 100px; width: 100px;" src="<?= $img['name'] ?>"> <?}?> <? if ($_POST[upload]) { $id = $_POST[id]; $uploaddir = '/i/'; file_put_contents($_SERVER["DOCUMENT_ROOT"]."/_.txt", "\n\r".__FILE__.":".__LINE__."\n\r".print_r($_FILES, true)); foreach($_FILES['userfile']['name']as $m = > $w) { if (($_FILES['userfile']['type'][$m] == 'image/gif' || $_FILES['userfile']['type'][$m] == 'image/jpeg' || $_FILES['userfile']['type'][$m] == 'image/png') && ($_FILES['userfile']['size'][$m] != 0 and $_FILES[ 'userfile']['size'][$m]<=1024000)) { $ex = explode('.', $w); $ex = end($ex); $apend = date('YmdHis').rand(100, 1000). '.'.$ex; $uploadfile = "$uploaddir$apend"; if (move_uploaded_file($_FILES['userfile']['tmp_name'][$m], $_SERVER[DOCUMENT_ROOT].$uploadfile)) { $c = "INSERT INTO images (name, tovars_id) VALUES('$uploadfile', '$id')"; $query = $dbh -> prepare($c); $query -> execute(); } } } header("Location: /pr1.php"); exit; } elseif($_POST[image_delete]) { $id = $_POST[image_id]; $src = $_POST[src]; $c = "DELETE FROM `images` WHERE id=$id"; //unlink($_SERVER[DOCUMENT_ROOT].$src); $query1 = $dbh -> prepare($c); $query1 -> execute(); } else { echo "Error2"; } ?> $(".modal_images").click(function() { id = this.dataset.id; src = this.attributes.src; //$.ajax("/exit.php", {image_id:id, src: src, image_delete: 'true'}/*, function (){$(this).fadeOut("slow")}*/); $.ajax({ type:"POST", url:"exit.php", data:{ image_id: id, src:src, image_delete:'true' }, success: function(msg) { alert(msg); } }); }); 
  • Are there many rows in the images table? - Alexey Shimansky
  • 3 fields (id. Name. Tovars_id) Many rows - Eliot
  • changed to $ id hangs still - Eliot
  • In my opinion, in such a code it is easier for you to find the error yourself .... comment out most of the code, reload the page .... if you "let go" uncomment gradually until you stumble upon something that will allow the page to be hung up .... ... try the request in the normal program .... how many lines will be issued ... is it a lot ... will the program not hang already on the Select request ........... otherwise you are now trying to return to a telepathic debugging session .... - Alexey Shimansky
  • @ Alexey Shimansky, commented on the ajax request, there is no freeze as well as deleting a picture. Only 4 pictures are output from the base, so there isn’t anything overloaded there. - Eliot

1 answer 1

Use standard POST request:

 $('#image').click(function (e) { var image = $(this).attr("src"); var delete_var = 'act=delete_image&image=' + image + ''; $.ajax({ type:"POST", url:"ajax.php", data:delete_var, success:function(data) { alert('deleted'); } }); }); 

ajax.php

 if ($_POST['act'] == 'delete_image') { $image = $_POST['image']; // название файла (src) // тут код удаления файла } 

Correct me, I do not know if I got the name of the picture correctly through Jquery. In general, the decision must be correct.