0

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>
5
  • and your question is? also: you should note that the blockit-function you provided does not take any arguments... Commented Nov 23, 2016 at 10:19
  • @FranzGleichmann The question is i want to alert value that is related to that button. like if 1st result have value 1 thn it should alert 1 so on Commented Nov 23, 2016 at 10:21
  • And different buttons with same id Commented Nov 23, 2016 at 10:21
  • what are you doing dude? if you are making function call like blockit(this.value) then use blockit(value) and alert(value)... simple Commented Nov 23, 2016 at 10:21
  • use class instead of id as you may have many buttons Commented Nov 23, 2016 at 10:29

5 Answers 5

2

pass the parameter to your custom function

function blockit(val)
{
  alert(val);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

0

try this

$(document).ready(function(){
   $("#block").click(function(){
     var a=$(this).val();
     alert(a);
  });
});

2 Comments

See how may buttons with same id as block
please change set class and use class in click event
0

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()

Comments

0
<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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.