I am developing a small website for a school project. On one page I iterate over an array which is populated via SQL query. The data will be displayed like this:
<ul class="list-group">
<li class="list-group-item" name="id" value="36"><span class="badge">9999</span>Testval</li>
</ul>
The name of that li
element is always static but the value is filled in with PHP. The value refers to the ID in the DB. I am trying to write a query for a delete statement which looks like this:
DELETE FROM `Kredit` WHERE ID=id;
The id
should be retrieved when the user clicks the delete button on that specific li element. There will be multiple li
elements which all have a different id. Is it possible to retrieve the id in the value attribute with PHP?
I searched on Stackoverflow for this question but all the questions were referring to the value between the li tags in my case "testval".
I am developing a small website for a school project. On one page I iterate over an array which is populated via SQL query. The data will be displayed like this:
<ul class="list-group">
<li class="list-group-item" name="id" value="36"><span class="badge">9999</span>Testval</li>
</ul>
The name of that li
element is always static but the value is filled in with PHP. The value refers to the ID in the DB. I am trying to write a query for a delete statement which looks like this:
DELETE FROM `Kredit` WHERE ID=id;
The id
should be retrieved when the user clicks the delete button on that specific li element. There will be multiple li
elements which all have a different id. Is it possible to retrieve the id in the value attribute with PHP?
I searched on Stackoverflow for this question but all the questions were referring to the value between the li tags in my case "testval".
Share Improve this question edited Dec 22, 2016 at 8:18 LF-DevJourney 28.6k30 gold badges165 silver badges316 bronze badges asked Dec 22, 2016 at 7:26 ArulociAruloci 5895 silver badges18 bronze badges 3- Could you add the plete html and where the form and delete button are setup? – Cesar Bielich Commented Dec 22, 2016 at 7:42
- if you want to do this action in PHP you have to include your PHP code – mike510a Commented Dec 22, 2016 at 7:45
- Why the tag salad on this question? Isn't this a PHP question? Why are there javascript answers? I find this question unclear in its requirements. – mickmackusa Commented Jul 9, 2024 at 22:57
3 Answers
Reset to default 5In my opinion you need Javascript to do this, jQuery code for this will loke like below. I dont see the delete button to be more specific, but i will explaine the code and hope to be clear enough for you.
$( "#delete" ).click(function() { // when delete is clicked
var id = $(this).attr("value"); // get id value of clicked li's delete
$.ajax({
type: 'GET',
cache: false,
aysync: true,
dataType: 'json',
data: { delete : id},
url: "ajax.php", // cal ajax.php with ?delete=123, use $_GET['delete']
success: function(data){
$(this).closest('li').remove(); // if ajax is done, remove this li
}
});
This example is for value id, i don't think you would like to search in DB by strings like testval instead of unique id.
You can use .attr
to get value
$(document).ready(function () {
$('.list-group-item').click(function () {
alert($(this).attr("value"));
});
});
value
is not a valid attribute
for li
but you can still get the value
but it would be better to use data-value
Nope. You have to pass the value from users side (js) via ppost/ajax to your backend (php). If youve got sth like this:
<form action="action.php" method="post">
<input name="id" id="submitter" value="00">
</form>
<li id="210">//your elem
... Another elems
<ul class="delete">Delete</ul>
</li>
Lets e to our js:
<script>
window.onload=function(){
Deleters=document.getElementsByClassName("delete");
for(i=0;i<Deleters.length;i++){
deleter=Deleters[i];
deleter.onclick=function(){
send(this.parentNode.id);//or .value
//this gets the id of the parents node (the li elem)
};
}
};
function send(value){
document.getElementById("submitter").value=value;
document.forms[0].submit();
}
</script>
Now you need a php to handle (call it action.php):
<?php
$delid=$_POST["id"]
....
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745602631a4635490.html
评论列表(0条)