Well, if I understand you correctly, you have problems understanding and concretizing which parts of these actions are server-side (PHP) and which parts are client-side (Javascript), and then the client-server communication strategy. This is the overall speed. There are several ways to handle this.
First (and less preferably) you can create a form and reload the entire page with a new date range:
// we're looking for '+1 year', '+3 months' or '+1 month'. if someone really // wants to send another value here, it not likely to be a security risk // but know your own application and note that you might want to validate $range = isset($_GET['range'])&&$_GET['range']?$_GET['range']:'+1 year'; $begin = new DateTime(date('Ym-d', strtotime('+1 days'))); $end = new DateTime(date('Ym-d', strtotime($range))); // ... the rest of your code to build the chart. ?> <form action="<?= $_SERVER['PHP_SELF']; ?>" method="get"> <select name="range" size="1"> <option value="+1 year">1 year</option> <option value="+3 months">3 months</option> <option value="+1 month">1 month</option> </select> <input type="submit" name="action" value="Redraw Chart"> </form>
... the reason why it is less preferable is that it causes a full page refresh.
If you want to avoid refreshing the entire page, you are doing pretty much the same thing, but you are doing it with ajax. The setup is almost identical, with only a few minor changes:
// between building the data table and the javascript to build the chart... $jsonTable = json_encode($table); if (isset($_GET['ajax']) && $_GET['ajax']) { echo json_encode(array('table' => $table)); exit; } // remainder of your code, then our new form from above ?> <form id="redraw_chart_form" action="<?= $_SERVER['PHP_SELF']; ?>" data-ajaxaction="forecast.php" method="get"> <? foreach ($_GET as $key => $val) { ?> <input type="hidden" name="<?= $key; ?>" value="<?= $val; ?>"> <? } ?> <input type="hidden" name="ajax" id="redraw_chart_form_ajax" value="0"> <select name="range" size="1"> <option value="+1 year">1 year</option> <option value="+3 months">3 months</option> <option value="+1 month">1 month</option> </select> <input type="submit" name="action" value="Redraw Chart"> </form> <script> </script>
Edit for clarity:
Remember to use the following code block from the first example (page refresh), otherwise you are not using the form at all:
$range = isset($_GET['range'])&&$_GET['range']?$_GET['range']:'+1 year';
$begin = new DateTime(date('Ym-d', strtotime('+1 days')));
$end = new DateTime(date('Ym-d', strtotime($range)));
Ajax will only work if the data you send back is a json-encoded block, which means that your charting data must be at the top of the script before any HTML output is run, including your page template. If you canβt put the charting code at the top of the script, then you will need to add it to a whole separate script, which all it does is calculate the data for the chart, and then you can let it return ajax data without all the rest of the HTML on the page . If you cannot do any of these things, you just need to disable the Ajax bit and refresh the whole page.
Edit 2: I added the data-ajaxaction to the <form> element, this is a user-defined attribute that I created to provide another action only for ajax. I also changed the call to $.ajax() to use this attribute, not the action attribute.
Jason
source share