-1

So I have tried to create a button from php that will be displayed in html and I tried to test the button by creating alert function but it doesn't work while if I create button in html directly the function works. Here is my code, which may not be clear, but I hope it helps with the understanding of my question. Thanks.

<?php 
echo"<div class='col-md-3'>";
echo"<p><button id='remove'>remove</button></p>";
?>

$(document).ready(function(){
$("#remove").click(function(){
        alert("test");

});
});
Kiran
  • 15
  • 1
  • 7

3 Answers3

1
<?php 
echo"<div class='col-md-3'>";
echo"<p><button id='remove'>remove</button></p>";
?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 

<script>
$(document).ready(function(){
$("#remove").click(function(){
    //$.post('store_items.php?del',function(data){
        alert("test");

});
});
</script>
clearshot66
  • 2,292
  • 1
  • 8
  • 17
0

You need to add script tags around the javascript code.

  <script type="javascript">
  $(document).ready(function(){
  $("#remove").click(function(){

    alert("test");

});
});
</script>

Also what type of file are you serving? is it a PHP file?

HCR
  • 1
  • 1
  • Welcome to SO. any clarification about the question should be asked in the comments section. Please see [this](http://meta.stackexchange.com/a/163589/338114) to understand when to post an answer and when to post a comment. – Sourav Ghosh Mar 22 '17 at 19:21
0

I just think you have to put things in the correct orders and in the right place. Try to reorganize your code as follow :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#remove").click(function(){
    //$.post('store_items.php?del',function(data){
        alert("test");

});
});
</script>
</head>
<body>

<?php 
echo "<div class='col-md-3'>";
echo "<p><button id='remove'>remove</button></p>";
echo "</div>";
?>

</body>
</html>

PS: I tried it and it work fine ;-)

Hope be helpfull good luck for the next

JVnal
  • 1
  • 1