如何让HTML中的按钮运行起来?

我的HTML中有一个按钮,当单击它时,我需要让它在PHP文件中运行一个查询,然后这个查询会回显结果。但是,当我单击按钮时,它什么也不做。这是怎么了?

HTML/Ajax:

<?php
  $reply_id = 15;
?>

<html>
  <body>
    <center><a class="btn show-more">Show more comments</a></center>
    <div id="resultcomments"></div>

    <script type="text/javascript">
      var id = $reply_id;

      $(document).ready(function() {    
        $(".show-more").click(function() {
          $.ajax({
            url: 'assets/misc/test.php',
            type: "POST",
            data: ({id_variable: id}),
            function(data) {
              $("#resultcomments").html(data);
            }
          });        
        });
      });
    </script>
  </body>
</html>

PHP (位于assets/misc/test.php中):

<?php

  $replyid= $_POST['id_variable'];

  echo $replyid;

  // query would go here

?>

since $reply_id is PHP variable, for using it inside javascript you need to do like:

var id = <?php echo $reply_id; ?>;
....

and change:

data: ({id_variable: id})

to

data: {id_variable: id}

like:

$.ajax({
     url: 'assets/misc/test.php',
     type: "POST",
     data: {id_variable: id},
     success: function(data) {
         $("#resultcomments").html(data);
     }
});

Try this in javascript

var id = <?php echo $reply_id; ?>;

No brackets needed for data attribute in ajax,

  data: {id_variable: id},

Try this for data attribute

You're not assigning your AJAX success function to a key, so I don't think it's ever firing. (You could try a console.log() or a debugger to check.) Try this instead:

$.ajax({
    url: 'assets/misc/test.php',
    type: "POST",
    data: {id_variable: id}
}).done(function (data) {
    $("#resultcomments").html(data);
});