I am trying to create an order system where once the user updates the status of the order, it updates it in the server as well without refreshing the page each time (as that seems tedious).
The trouble is, I am generating javascript errors onchange:
Uncaught ReferenceError
Here is the function that displays the orders:
function showOrders()
{
global $con;
$getOrders = "SELECT * FROM `Purchase_Order`";
$rungetOrders = mysqli_query($con, $getOrders);
$getOrder = "SELECT * FROM `Purchase_Order` WHERE status = 'pending' ORDER BY date ASC";
$rungetOrders = mysqli_query($con, $getOrder);
echo '<form action ="PurchaseOrders.php">
<table style="border :1px solid black; border-collapse: collapse; width: 1300px; margin-bottom: 50px; margin-top: 5px; font-size:20px ">
<tr style="border :1px solid black; background-color: black; color: whitesmoke">
<th>ORDER #</th>
<th>PRODUCT ID</th>
<th>COLOR</th>
<th>SIZE</th>
<th>QTY</th>
<th>DATE ORDERED</th>
<th>STATUS</th>
</tr>';
$counter = 0;
while($row = mysqli_fetch_array($rungetOrders))
{
$ordernum = $row['OrderNum'];
$product_id = $row['product_id'];
$product_color = $row['product_color'];
$product_size = $row['product_size'];
$product_quantity = $row['product_quantity'];
$date = $row['date'];
$status = $row['status'];
if($counter% 2 == 0)
{
$bg = 'rgba(50,205,50, 0.2);';
} else {
$bg = '';
}
if ($status === "shipped")
{
$newstat = "pending";
} else {
$newstat = "shipped";
}
echo '<tr style="background-color:'.$bg.'">
<td>'.$ordernum.'</td>
<td>'.$product_id.'</td>
<td>'.$product_size.'</td>
<td>'.$product_color.'</td>
<td>'.$product_quantity.'</td>
<td>'.$date.'</td>
<td><select name="status" onchange="ship('.$ordernum.','.$product_id.')"><option>'.$status.'</option><option>'.$newstat.'</option</td>
</tr>';
$counter++;
}
}
Here is the ajax that I put on the page where the function is being executed:
<script type="text/javascript">
function ship(order,id) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
alert('triggered');
};
xhttp.open("GET", "updateStatus.php?update=false&order="+order+"&id="+id, true);
xhttp.send();
}
</script>
Here is what I have in the updateStatus.php file:
if(isset($_GET['update']))
{
$update = $_GET['update'];
$id = $_GET['id'];
$order = $_GET['order'];
$make = "UPDATE `Purchase_Order` SET `status`='shipped' WHERE `OrderNum` = $order AND `product_id` = $id";
$runmake = mysqli_query($con, $make);
if($runmake)
{
echo "go";
} else {
echo "stop";
}
}
I am very new to AJAX. Please help me to understand what I am doing wrong.