0

I am getting dynamic buttons from database the thing is,I want to use the value from the created button to fetch data,but when I click the button,on the value of the first button is passed even when i click a different button.here is my js code to get the button value.

function prodata1()
     {
       $(document).ready(function(){
           $("#show").click(function(){
                 var show=$("#show").val();
                 $.ajax({
                    type:"post",
                    url:"assets/exec/compare-exec.php",
                    data:"show="+show,
                    success:function(data){
                    $("#link2").html(data);
                    }
                   });
              });
         });
       }
5
  • Could you show us what these dynamically created buttons look like in html? Commented Aug 6, 2015 at 4:26
  • can you give the code for how you are adding the buttons? Commented Aug 6, 2015 at 4:27
  • while($data=$stmt->fetch()) { extract($data); ?> <div align="left"> <span class="name"><?php echo $data['pname']; ?></span>&nbsp;<br/><?php echo $data['pmanu']; ?><br/> <!--<input type="text" id="show" onclick="prodata1()" name="link1" value="//">>--> <button id = "show" name="show"value="<?php echo $data['pid']; ?>" onclick="javascript:prodata1()">clickme</button> <a class="btn btn-success" value= "<?php echo $data['pid']; ?>" ><?php echo $data['pid']; ?></a> </div> <?php } Commented Aug 6, 2015 at 4:29
  • Please update the question with the code instead of commenting it, reading the structure is difficult in comments Commented Aug 6, 2015 at 4:29
  • use firebug to see the ajaxs input and output, it will better help you with why something isnt working right. Commented Aug 6, 2015 at 4:55

2 Answers 2

3

Probably you are using same id(#show) in all buttons. ID must be unique in a page. Give different names in id or try to use class. and change your code to:

    function prodata1()
    {
       $(document).ready(function(){
           $(".show").click(function(){
                 var show=$(this).val();
                 $.ajax({
                    type:"post",
                    url:"assets/exec/compare-exec.php",
                    data:"show="+show,
                    success:function(data){
                    $("#link2").html(data);
                    }
                   });
              });
         });
       }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot guys.chayan's answer has helped me out.Thanks alot guys you are the best.
0

You have a lot of unnecessary code in there, this should work. You shouldn't have more than 1 element with the same ID.

I've changed the ID to a class and also passing in the element reference on the click

<button class="show" name="show"value="<?php echo $data['pid']; ?>" onclick="javascript:prodata1(this)">clickme</button>

Capturing the click with e you can easily get the value with e.value, also the way you had structured the data was incorrect.

function prodata1(e) {
  $.ajax({
    type:"post",
    url:"assets/exec/compare-exec.php",
    data: {show: e.value},
    success:function(data){
      $("#link2").html(data);
    }
  });
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.