-1

I have a button within my HTML that, when you click it, I need to have it run a query in a PHP file which would then echo the results.

I've tried the following, however, when I click the button, it does nothing. What's wrong?

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 (located in assets/misc/test.php):

<?php

  $replyid= $_POST['id_variable'];

  echo $replyid;

  // query would go here

?>
Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
John
  • 81
  • 8
  • FYI: If you plan on using `15` as a text variable then use `$reply_id = "15";` because otherwise, it will be treated as a number/integer and not a string. – Funk Forty Niner Sep 02 '13 at 04:16
  • 3
    You should echo the variable in your js like this: `var id = ;` – ezakto Sep 02 '13 at 04:16
  • @Fred -ii-: and what could it cause? – zerkms Sep 02 '13 at 04:17
  • @zerkms I thought there would be a conflict when using a number as an ID (text) instead of an integer. I remember seeing a similar question last week, but can't remember the URL on SO. From as much as I can remember, ID's shouldn't start with a number, which could be the case here. – Funk Forty Niner Sep 02 '13 at 04:24
  • @Fred -ii-: I'm sure it was about html `id` attribute, not javascript. See http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html – zerkms Sep 02 '13 at 04:53
  • @zerkms Yes you're right. I take it that it has no bearing on this question then(?) – Funk Forty Niner Sep 02 '13 at 04:54

4 Answers4

2

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);
     }
});
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1

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);
});
Sam Blake
  • 657
  • 6
  • 13
0

Try this in javascript

var id = <?php echo $reply_id; ?>;
Satish Ravipati
  • 1,431
  • 7
  • 25
  • 40
0

No brackets needed for data attribute in ajax,

  data: {id_variable: id},

Try this for data attribute

Manu M
  • 1,074
  • 5
  • 17