Vinita Vaswani Vinita Vaswani - 4 months ago 10
MySQL Question

fetch data using ajax on date selection with php

I am trying to fetch data on change of date using date type,have fetched data from database. But after that, the data that is fetched contains button to be clicked. On click of button fetch the data again.

here is the code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>
<form method="post">
<input type="date" name="gdate" id="gdate" onchange="<?php echo $_SERVER['PHP_SELF']?>"/>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function makeAjaxRequest(dateval){
$.ajax({
type: "GET",
data: { dateva: dateval },
url: "fetchbills.php",
success: function(data) {
$('#showresult').html(data);
$('#det').on("click",function(){
var no = $(this).val();
makeAjaxRequest2(no);
});
}
});
}
function makeAjaxRequest2(billno){
$.ajax({
type: "GET",
data: { bno: billno},
url: "fetchbilldetail.php",
success: function(data) {
$('#showresult').html(data);
}
});
}
$("#gdate").on("change", function(){
var id = $(this).val();
makeAjaxRequest(id);

});
</script>
<div id="showresult">

</div>
</body>
</html>


fetchbills.php

<?php
include("./config.php");
$dt = $_REQUEST["dateva"];
$sql="select bill_id,order_id from bill_master where order_date='$dt'";
echo $sql;
$result=mysqli_query($con,$sql) or die("error");
if($result)
{
while($row=mysqli_fetch_array($result))
{
echo $row[0]."<br>";
echo "<form method='post' action=''><input type='submit' name='det' value='".$row[1]."' onclick='get(this.value)' /></form>
<div id='show'></div>";
}
}
?>


using the above I get order id.The id's are displayed in form of buttons.
On click of buttons I want to get the details of order id.but what sholud I do I can't understand.

I am new to ajax and don,t know how to do it.

here the the code for order details

<?php
include("./config.php");
$det = $_GET["bno"];
//echo $det;
$sql="SELECT m.old_order_id,m.old_order_amount,l.item_id,l.old_order_quantity,i.item_name from old_order_master m,old_order_list l,item_list i where m.old_order_id=$det and m.old_order_id=l.old_order_id and l.item_id=i.item_id";
//echo $sql;
$result=mysqli_query($con,$sql);
echo "<table><tr><th>Item Name</th><th>Quantity</th></tr>";
while($row=mysqli_fetch_array($result))
{

echo "<tr><td>".$row[4]."</td><td>".$row[3]."</td></tr>";
}
echo "</table>";
?>

Answer

Make following changes in your fetchbills.php file and let me know

<?php

include("./config.php");

$dt = $_REQUEST["dateva"];
$sql = "select bill_id,order_id from bill_master where order_date='$dt'";

$result = mysqli_query($con, $sql) or die("error");

if ($result) {
    while ($row = mysqli_fetch_array($result)) {

        echo $row[0] . "<br>";
        echo "<form method='post' action=''>"
            ."<input type='button' name='det' value='" . $row[1] . "' onclick='makeAjaxRequest2(".$row[1].");' /></form>"
            ."<div id='show'></div>";
    }
}
Comments