将PHP值作为参数传递给jQuery并在alert [duplicate]中显示该值

This question already has an answer here:

Hello i want to display the value in alert . I passed the php value as a parameter in jquery and when i display that value in alert it shows empty Code is here

<button type="button" onclick="topSharedPosts('<?php echo $postID; ?>')" class="btn btn-success" value="<?php echo $postID; ?>"> Facebook </button>

Jquery Function

function topSharedPosts(id) {
       alert(id);
}
</div>

It's working for me:-

  1. your file must have .php extension not .html.

Check the below code:-

Alert.php:-

<?php 
$postID = 2;
?>
<button type="button" onclick="topSharedPosts('<?php echo $postID; ?>')" class="btn btn-success" value="<?php echo $postID; ?>"> Facebook </button>

<script type = "text/javascript">
function topSharedPosts(id) {
       alert(id);
}
</script>

Note:- there is no jquery in your code and my code. It's pure javascript. I have taken $postId value to check that code works or not, and it works fine.