Thank you Artan. I realized I haven't posted my entire code which makes it hard to diagnose the problem. Your suggestion pointed me in the right direction though. For others who may be looking at doing the same thing I'll post my entire code below and try to add comments. Hopefully that can save you some time. Also, if there are better ways to do this please feel free to comment.
-------------------------------------I'm using bootstrap but left out all standard stuff like the scripts and css links. This page is just a basic form with checkboxes and an svg element where the chart is generated--------------------------------------------
<body>
<div class="container">
<form action="" method="post" id="form">
<input type="checkbox" name="Location[]" value="AB">AB<br>
<input type="checkbox" name="Location[]" value="CD">CD<br>
<input type="submit" name="submit" value="submit" id="submit">
</form>
</div>
<div class="container">
<svg id="chart"></svg>
</div>
</body>
</html>
PHP:
------------------------------------------------------------------------------------------------------------Here I just want to query my mysql database and return a default starting result set and then run the query again based on the data submitted in the form---------------------------------------------------------------
<?php
$hostname = 'localhost';
$db = '***';
$username = '****';
$password = '*****';
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password, array(
PDO::ATTR_PERSISTENT => true
));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST['ajax']) && $_POST['ajax'] === '1') { //ajax=1 is added to form data when submitted in order to check if submit has occured. I realize this could represent a problem if form is submitted without a checkbox selection. I'll add a fix for this later
$values = $_POST['Location'];
$qmarks = str_repeat('?,', count($values) - 1) . '?';
$sql = $dbh->prepare("SELECT * FROM cases WHERE location IN ($qmarks) AND date between '2014-01-01' AND '2014-01-31'");
foreach($values as $k => $value) {
$sql->bindValue(($k+1), $value);
}
$sql->execute();
$results = $sql->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: application/json');
echo json_encode($results);
} else { // default query runs when form hasn't been submitted
$sql = $dbh->prepare("SELECT * FROM cases where location = 'AB' AND date between '2014-01-01' AND '2014-01-31'");
$sql->execute();
$results = $sql->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: application/json');
echo json_encode($results);
}
Returned Data:
[{"location":"AB","Date":"2014-01-01 00:00:00","Value":"50"},{"location":"CD","Date":"2014-01-01 00:00:00","Value":"75"},............]
Javascript:
-----------------------------------------------------I am using an external file for my d3 code. The solution I finally came up with was to combine the onclick and submit event to send the form data to my PHP script and then grab the data right in the callback function rather than do it separately as I was trying to do in the past.----------------------------------------------------
var margin = {top:15, right:15, bottom:15, left:15},
height = 500 - margin.top - margin.bottom,
width = 900 - margin.left - margin.right;
var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
var formatTime = d3.time.format("%H:%M:%S"); //Not doing anything with this yet as I haven't added axis.
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var chart = d3.select("#chart")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json("getdata.php", function(error, data) { //Loading the default data from PHP file.
data.forEach(function(d) {
d.Date = parseDate(d.Date);
d.Value = +d.Value;
});
x.domain(data.map(function(d) { return d.Date; }));
y.domain([0, d3.max(data, function(d) { return d.Value; })]);
chart.selectAll(".bar")