如何在按下javascript的onclick函数时传递varchar参数? [重复]
This question already has an answer here:
May I know how to pass a varchar parameter to an onClick function, the varchar parameter is the uuid that stored in my database table and i use mysqli_fetch_array to retrieve them. A list of items will display in my html and each item is assigned with the button, when the button is pressed , it will display the uuid of the particular item in the alert window. I am able to that when the id is numeric or integer, but when I change it into the uuid or md5 id or the string, the value is failed to pass, and the button is stuck. Here is some part of my code.
<button type="button" class="btn btn-outline btn-danger"
onClick="deletefun(<?php echo $row["id"]; ?>)"
title="Delete">delete</button>
When the deletefun button is pressed, the alert window will pop out to display the uuid of the particular item in the list.
<script>function deletefun(data){
alert(data);
</script>
Thank you for helping.
</div>
Just add quotes around <?php echo $row["id"]; ?>
in your button :
<button type="button" class="btn btn-outline btn-danger"
onClick="deletefun('<?php echo $row["id"]; ?>')"
title="Delete">delete</button>
Without these quotes, the function treats <?php echo $row["id"]; ?>
as if it was the name of a variable instead of a string.