3

I would like to start by saying that i am completely new to Ajax so please bear with me. I have to calculate the sum of all the prices sold between the two dates taken as an input.

Here is my table

     Product_ID----ProductName------price------OrderDate
    ---  1  --------  Chair -------- 7 ------ 2015-01-05
    ---  2  --------  Lamp --------- 14 ----- 2015-01-16
    ---  3  --------  Table -------- 9 ------ 2015-02-25

Here's my HTML

<html>
<head>
<script>
function showPrice(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getprice.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<form>
 <input type="date" name="startdate">
 <p>To</p>
 <input type="date" name="enddate">
 <br><br>
 <button type="submit" name="submit"       
onclick="showAge(this.value)">Calculate</button>

</form>
<br>
<div id="txtHint"><b>Age  will be listed here...</b></div>

</body>
</html>

And here is my MySQL query

SELECT SUM(price) AS TotalPrice 
FROM orders
WHERE OrderDate >= '11/11/2014' AND OrderDate <= '15/11/2015'

So my question is, how can i send two dates as input using ajax to calculate the sum of prices?

4
  • That query will not work. Commented Apr 7, 2015 at 10:46
  • What is your problem? What have you tried to do about it? Please improve your question by adding more info on the actual thing blocking you. Commented Apr 7, 2015 at 10:47
  • Database allow only Y-m-d date format so convert your Date accordingly Commented Apr 7, 2015 at 10:50
  • @SunilPachlangia, this query works since i have tested it.. I need to know how do i need to send two dates in a Ajax method to compute the sum of prices. Thanks Commented Apr 7, 2015 at 10:52

2 Answers 2

1

Change this line to,

xmlhttp.open("GET","getprice.php?q="+str,true);

xmlhttp.open("GET","getprice.php?q1="+str1+"&q2="+str2,true); 

and str1 and str2 will be your both dates, 11/11/2014 & 15/11/2015. you must modify this date to Y-m-d format.

You can convert the date like this,

$date = '10.21.2011';
echo date('Y-m-d', strtotime(str_replace('.', '/', $date)));
Sign up to request clarification or add additional context in comments.

Comments

1

try like this,

var date1 = $('#startdate').val();
var date2 = $('#enddate').val();

    xmlhttp.open("GET","getprice.php?q1="+date11+"&q2="+date2,true);

then format date to 'Y-m-d' and pass it to your query

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.