-1

I am trying to open a bootstrap modal on button click but it appears my code is not running. Here is my code snippet:

<button type="button" class="btn btn-xs btn-default" onclick="openViewUserModal(<?= $users['userid']; ?>); return false;">
  <span class="glyphicon glyphicon-eye-open"></span>
</button>
function openViewUserModal(id) {
  var data = {
    "id": id
  };
  jQuery.ajax({
    url: "includes/viewUser.php",
    method: "POST",
    data: data,
    success: function(data) {
      jQuery('body').append(data);
      jQuery('#viewUserModal').modal({
        backdrop: 'static',
        keyboard: false,
        show: true
      });
    }
  });
}

Clicking on the button does not elicit a response. What am I doing wrong?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
donsonde
  • 87
  • 8

2 Answers2

0

this should work, mark the echo statement. i think its a typo.

<button type="button" class="btn btn-xs btn-default" onclick="openViewUserModal('<?php echo $users['userid']; ?>');">
  <span class="glyphicon glyphicon-eye-open"></span>
</button>
Sekhu
  • 139
  • 1
  • 12
0

Assuming that the output of <?= $users['userid']; ?> is a string then you'll need to wrap it in quotes:

onclick="openViewUserModal('<?= $users['userid']; ?>'); return false;"

That being said, using inline event handlers is a very outdated method which should be avoided possible. Use unobtrusive event handlers instead. You can provide arguments to that event handler through data attributes, something like this:

$('.btn').click(function() {
  $.ajax({
    url: "includes/viewUser.php",
    method: "POST",
    data: {
      "id": $(this).data('userid')
    },
    success: function(data) {
      $('body').append(data);
      $('#viewUserModal').modal({
        backdrop: 'static',
        keyboard: false,
        show: true
      });
    }
  });
});
<button type="button" class="btn btn-xs btn-default" data-userid="<?= $users['userid']; ?>">
  <span class="glyphicon glyphicon-eye-open"></span>
</button>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339