I am making an app that procedurally generates HTML code from MySQLi database results. It uses a button that calls a JavaScript function that sends an XMLHttpRequest to the server that deletes the row to remove items from the list once they have been processed. Here is the relevant PHP code:
<?php
require_once("lib/dbclass.php");
$conn = new Conn();
$result = $conn->query("SELECT * FROM orders");
?>
<?php foreach($result as $item) {?>
<div class="row" id="item<?php echo $item['oid']; ?>">
<p>Type</p>
<span class="item"><?php echo htmlspecialchars($item['a']); ?></span>
<p>Size</p>
<span class="item"><?php echo htmlspecialchars($item['b']); ?></span><br>
<button type="button" onclick="close('<?php echo $item['c']; ?>')">Item processed</button>
</div>
<?php }?>
<script src="js/dashboard.js"></script>
And here is dashboard.js:
function close(order_id) {
console.log("Closing order number " + order_id);
var http = new XMLHttpRequest();
http.onreadystatechange = function() {
console.log("Readystate changed! New: " + this.readyState);
if (this.readyState == 4 && this.status == 200) {
console.log("Operation completed.");
document.getElementById("order" + order_id).remove();
}
}
http.open("GET", "order_complete.php?oid=" + order_id, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send();
}
However, the problem is that onclick does not call the close function. In the inspect element panel, it shows up correctly as <button type="button" onclick="close(2)")>Item processed</button>
. but, when I click the button, nothing happens. I have tried:
- Explicitly declaring the button as a button using
type="button"
. Made no difference. - Removing the parameter from the
onclick
tag so it looked likeonclick="close(1)"
. Made no difference. - Removing all styling from the button. Made no difference.
- Calling the function from the console. It worked there, and removed the item from the page. So it seems the button is the problem.
- Changing the
onclick
of the button from the inspect panel toalert("Hi")
. The function called when the button was pressed. When I changed it back toclose(2)
it calledalert("Hi")
again.