Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update table data with array from PHP function
    text
    copied!<p>I'm building a simple budgeting app, and I'm trying to filter a table of results based upon a dropdown menu with the months of the year in it. When the page loads it fires off a php function that gets all the budget categories in the current month, and that's working fine. What I want to do is be able to select a different month from the dropdown menu and then update the table with only items from that month. </p> <p><strong>How could I update the table with only the selected month's data?</strong> </p> <p>Here's my AJAX call that will successfully alert the array of filtered results, but that's about as far as I know how to get. I don't know how to take the AJAX response and feed it into the table.</p> <pre><code>function filterCategory() { var cur_month = $('#month option:selected').attr("value"); $.ajax({ type: "POST", url: 'inc/functions.php', data: {action: "filter_categories", cur_month:cur_month}, success: function(response){ alert(response); } }); }; </code></pre> <p>here's my PHP functions, one for getting the initial list of income and expense categories:</p> <pre><code>function get_income_cat() { require(ROOT_PATH . "inc/database.php"); $cur_month = date('m'); try { $results = $db-&gt;query("SELECT * FROM categories WHERE is_income = '1' AND month ='" . $cur_month . "' ORDER BY cat_name ASC;"); } catch (Exception $e) { echo ("ERROR: Data could not be retrieved from the database." . $e); exit; } $income = $results-&gt;fetchall(PDO::FETCH_ASSOC); return $income; }; function get_expense_cat() { require(ROOT_PATH . "inc/database.php"); $cur_month = date('m'); try { $results = $db-&gt;query("SELECT * FROM categories WHERE is_income = '0' AND month ='" . $cur_month . "' ORDER BY cat_name ASC;"); } catch (Exception $e) { echo ("ERROR: Data could not be retrieved from the database." . $e); exit; } $expense = $results-&gt;fetchall(PDO::FETCH_ASSOC); return $expense; }; </code></pre> <p>and here's my main php/html code:</p> <pre><code>&lt;?php foreach($income as $key=&gt;$value) { ?&gt; &lt;tr&gt; &lt;td id="&lt;?php echo $value['cat_id_PK']; ?&gt;"&gt;&lt;?php echo $value['cat_name']; ?&gt;&lt;/td&gt; &lt;td id="&lt;?php echo $value['cat_id_PK']; ?&gt;"&gt;&amp;#36;&lt;?php echo $value['cat_amount']; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php }; ?&gt; &lt;?php foreach($expense as $key=&gt;$value) { ?&gt; &lt;tr&gt; &lt;td id="&lt;?php echo $value['cat_id_PK']; ?&gt;"&gt;&lt;?php echo $value['cat_name']; ?&gt;&lt;/td&gt; &lt;td id="&lt;?php echo $value['cat_id_PK']; ?&gt;"&gt;&amp;#36;&lt;?php echo $value['cat_amount']; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php }; ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload