[英]Delete element from table by id with button php
I have this table: 我有这张桌子:
Here's code of this page: 这是此页面的代码:
<?php
include('footer.php');
include('../models/fetchQuotes.php');
$content = file_get_contents("http://test/MY_API/getAllTopics");
$arrayId = array();
$arrayName = array();
$arrayImg = array();
foreach (json_decode($content, true) as $eventrType => $events) {
array_push($arrayId, $events[id]);
array_push($arrayName, $events[name]);
array_push($arrayImg, $events[img]);
}
?>
<div class="container">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Img</th>
<th>Option 1</th>
</tr>
</thead>
<tbody>
<?php for ($i=0;$i<count($arrayId);$i++) { ?>
<tr>
<td><?php echo ($arrayId[$i])." "; ?></td>
<td><?php echo ($arrayName[$i])." "; ?></td>
<td><img src="<?php echo ($arrayImg[$i])." ";?>" alt="" width="75", heigth="75"></td>
<td> <button class="btn btn-danger" id="deleteById" value=<?= ($arrayId[$i]); ?> onclick="myFunction()">DELETE</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Ошибка</h4>
</div>
<div class="modal-body" id ="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Закрыть</button>
</div>
</div>
</div>
</div></td>
</tr><?php } ?>
</tbody>
</table>
</div>
<script>
function myFunction(){
var deleteById = document.getElementById('deleteById').value;
alert(deleteById);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
I parse my own API and then fill it into a table. 我解析自己的API,然后将其填充到表中。 Now when I click on any button DELETE, every time I have the same alert "12".
现在,当我单击任何按钮DELETE时,每次我都有相同的警报“ 12”。 I understand why its happen, but i can't figure out how make it correct.
我知道为什么会发生,但是我不知道该怎么做。 How do I associate each button with the corresponding cell ID?
如何将每个按钮与相应的单元格ID关联? Sorry for language mistakes and thanks for helping.
抱歉语言错误,感谢您的帮助。
The problem is that you can only have only one id in a page, but as you are giving that button an id inside a loop different elements are getting same id. 问题在于,页面中只能有一个id,但是当您在循环中为该按钮指定一个id时,不同的元素将获得相同的id。
To fix this, you can always use class. 要解决此问题,您可以随时使用class。 But your according to your approach use something like this.
但是您根据自己的方法使用类似这样的东西。
<button class="btn btn-danger" onclick="myFunction(<?= ($arrayId[$i]); ?>)">DELETE</button>
And in javascript 并且在javascript中
function myFunction(id){
alert(id);
}
I hope this helps you. 我希望这可以帮助你。
Cheers :) 干杯:)
I advise you to pass current ID as a function parameter: 我建议您传递当前ID作为函数参数:
<button class="btn btn-danger" value=<?= ($arrayId[$i]); ?> onclick="myFunction(<?= ($arrayId[$i]); ?>)">DELETE</button>
And function signature will be: 函数签名将是:
function myFunction(idToDelete){
alert(idToDelete);
}
Also I delete id
attribute from button
as it's not required. 我也从
button
删除id
属性,因为它不是必需的。 And if you want to use same id for multiple elements in future - don't , as id must be unique on html-page. 而且,如果您以后希望对多个元素使用相同的ID, 请不要使用 ,因为ID在html页上必须是唯一的 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.