New to web dev so bear with me. In wordpress I have a functions.php file with a function that generates a chart and would take 2 dates as an input.
In my page-template.php I would take 2 date inputs and click generate to make a chart re-generate without reloading the page. I have a function with no arguments that runs on page load, and the button would load the alternate function that takes the two dates on button click.
Ajax seems to be the answer but most tutorials confuse me as their example require connecting to the DB or is focused on it exclusively. Wouldn't calling my function that does it already via wordpress functions handle that on their own? Is there a simple way to get my function called as it is while passing the two form values date1,date2? Truncated code to be hopefully readable.
Appreciate the help.
page-template.php: The first function is the one on load with default range (from functions.php file), then the date picker and button.
<?php
lineChartAll_Generate();
echo "<form>
Select Date Range:
<input type='date' name='date1'>
<input type='date' name='date2'>
</form>";
?>
functions.php: Using WP database function to run a select into a variable, then run the javascript code that generates the visuals of my chart.
<?php
add_action('wp_enqueue_scripts','enqueue_parent_styles');
function enqueue_parent_styles(){
wp_enqueue_style('parent-style', get_template_directory_uri().'/style.css');
}
function lineChartCustom_Generate($date1,$date2){
//Database queries
global $wpdb;
$overallenergyResults = $wpdb->get_results
(
"SELECT QUERY GOES HERE"
);
// Line chart code
echo "
<div class='lineChartAll'>
<canvas id='myChart' width='400' height='200'></canvas>
<script>
//Chart javascript code goes here using $overallenergyResults variable
</script></div>";
}
?>