I want to alert id when user click on the button.But i am getting the same id on every button whereas when i inspect element it have different values. This is a part of my code
Fetching code:
$sql="select * from `table_name`";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));
while($row=mysqli_fetch_array($result))
{
<button type="button" class="btn btn-danger" id="block" onclick="blockit(this.value);" value="<?php echo $row['id'];?>">Block</button>
Query:
<script>
function blockit()
{
var a=document.getElementById("block").value;
alert(a);
}
</script>
pass the parameter to your custom function
function blockit(val)
{
alert(val);
}
For your every button
you id is same. from line,
<button type="button" class="btn btn-danger" id="block" onclick="blockit(this.value);" value="<?php echo $row['id'];?>">Block</button>
Do remember, id
should be unique. Since your id
is same for all, it always consider only one, and it will neglect others. And hence you are getting same value.
Try with below code,
$sql="select * from `table_name`";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));
while($row=mysqli_fetch_array($result))
{
<button type="button" class="btn btn-danger" onclick="blockit(this.value);" value="<?php echo $row['id'];?>">Block</button>
JS,
<script>
function blockit(value){
alert(value);
}
</script>
try this
$(document).ready(function(){
$("#block").click(function(){
var a=$(this).val();
alert(a);
});
});
Just change your BUTTON code as below,
while($row= mysqli_fetch_array($result))
{
$ID= $row['id'];
<button type="button" value="<?php echo $ID;?>" onclick="alert('<?php echo $ID;?>')" class="btn btn-danger" id="block" > Block</button>
You don't need to use blockit()
<button type="button" class="btn btn-danger btn_block" onclick="blockit(this.value);" value="<?php echo $row['id'];?>">Block</button>
$(document).ready(function(){
$(".btn_block").click(function(){
var a=$(this).val();
alert(a);
});
});
try this
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.