Tell me how to make the removal of a picture in a modal window. You need to delete from the database (the path of the picture).

There is a table of images (contains pictures of goods) and the table has 3 fields :

  1. id - A_U field
  2. name - (varchar 255) contains the path to the image "/i/20160418.jpg"
  3. tovars_id - associates a picture with a product. (1 product I can have several pictures)

In general, I have a problem with the removal. The code is written. The problem is this when you click on the picture below (I circled around with a red square in the screenshot) my whole site hangs and nothing can be done, I can’t even see what comes up in the Network tab (Google Chrome browser).

Pictures in a modal window are displayed by foreach loop through php.

$_POST[upload]- это ΠΊΠ½ΠΎΠΏΠΊΠ° Π—Π°Π³Ρ€ΡƒΠ·ΠΈΡ‚ΡŒ if($_POST[upload]) { $id=$_POST[id]; $uploaddir = '/i/'; 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"; $query1=$dbh->prepare($c); $query1->execute(); } else{ echo"Error2"; } 

Ajax

 $(".modal_images").click(function(){ id=this.dataset.id; //ΠΏΠΎΠ»ΡƒΡ‡Π°ΡŽ id ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ src=this.attributes.src;// ΠΏΠΎΠ»ΡƒΡ‡Π°ΡŽ ΠΏΡƒΡ‚ΡŒ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ $.ajax({ type: "POST", url: "exit.php", data: {image_id:id, src: src, image_delete: 'true'} , success: function(msg){ alert( "ΠŸΡ€ΠΈΠ±Ρ‹Π»ΠΈ Π΄Π°Π½Π½Ρ‹Π΅: " + msg ); } }); }); 

HTML & PHP

Here are the pictures from the database.

 <? foreach($rows as $k=>$img){ ?> <img id="output_<?=$k?>" data-id="<?=$img[id]?>" class="modal_images" style="height: 100px; width: 100px;" src="<?= $img['name'] ?>"> <?}?> 


People help to understand why the page hangs and does not work click on the picture. I do not know how to solve my problem. Maybe the modal window is not suitable for solving this problem or there is another theory.

    1 answer 1

    If the entire page hangs along with the console, then the interface hangs, caused by JavaScript code. For example, a while loop in which a condition always works. There are no such cycles on this piece of code, so maybe someone else is listening to a click on the image. Perhaps a plug-in that implements a modal window listens to a click on itself and somehow behaves incorrectly. Especially true if the plugin samopisny. From this code, an interface hangup can create (and creates) only an alert, but you should see a chrome window at the same time. It is an unlikely situation, but for me, for example, Ubunt loves to make the monitor attached to the laptop mainly in a bundle and sends all the alarms to him, although this does not apply to chromium alerts. I would just in case replace alert () with console.log () and in the future do all debugging through the console. To learn something further you need more data.

    • Can you suggest an alternative picture deletion? - Eliot