Request class is used for advanced users who find that our built-in callback and updatepanel are not enough and want to build a custom ajax request.
In above example, upon selection of customer, the customerNumber will be sent to server. At server-side, the orders of that customer will be queried and sent back to client-side in table format. At client-side, on receiving the response from server, the onDone event handle will be called to display out the result.
<?php
require $KoolControlsFolder."/KoolAjax/koolajax.php";
$koolajax->scriptFolder = $KoolControlsFolder."/KoolAjax";
echo $koolajax->Render();
?>
<form is="form1" method="post">
Select customer to view order details:
<select id="cbbCustomer" onchange="select_customer()">
<option value="">--</option>
<?php
$result = mysqli_query($db_con, "select customerNumber,customerName from customers limit 0,10");
while($row= mysqli_fetch_assoc($result))
{
echo "<option value='".$row["customerNumber"]."'>".$row["customerName"]."</option>";
}
?>
</select>
<div id="orderSummary" style="padding-top:10px;width:300px;"></div>
<script type="text/javascript">
function select_customer()
{
var request = new KoolAjaxRequest({
method:"post",
url:"Process.php",
onDone:function(result){
//Show orders of customers
document.getElementById("orderSummary").innerHTML = result;
}
});
var _customerNumner = document.getElementById("cbbCustomer").value;
if(_customerNumner!="")
{
request.addArg("customerNumber",_customerNumner);
koolajax.sendRequest(request);
}
}
</script>
</form>
<?php
require "../../../../Resources/config.php";
$db_con = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
$customerNumber = $_POST["customerNumber"];
$result = mysqli_query($db_con, "select orderNumber,orderDate, status from orders where customerNumber=$customerNumber");
echo "<table border='1'>";
echo "<tr><th>Order Number</th><th>Order Date</th><th>Status</th></tr>";
while($row=mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row["orderNumber"]."</td>";
echo "<td>".$row["orderDate"]."</td>";
echo "<td>".$row["status"]."</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($db_con);
?>