Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery getJSON populate select menu question
    text
    copied!<p>I am populating a select menu with getJSON. I am wondering if there's a way that I can use jQuery's .each function to bring in these values?</p> <p>Surely there must be an easier way to accomplish this...maybe?</p> <p>PHP file:</p> <pre><code>&lt;?php $queryMonth = "SELECT monthID, month FROM months"; $result = $db-&gt;query($queryMonth); while($rowMonth = $db-&gt;fetch_assoc($result)) : $data[] = $rowMonth; endwhile; echo json_encode($data); ?&gt; </code></pre> <p>The jQuery:</p> <pre><code> $.getJSON('selectMenus.php', function(data){ $("select.month").append("&lt;option value=" + data[0].monthID + "&gt;" + data[0].month + "&lt;/option&gt;"); $("select.month").append("&lt;option value=" + data[1].monthID + "&gt;" + data[1].month + "&lt;/option&gt;"); $("select.month").append("&lt;option value=" + data[2].monthID + "&gt;" + data[2].month + "&lt;/option&gt;"); $("select.month").append("&lt;option value=" + data[3].monthID + "&gt;" + data[3].month + "&lt;/option&gt;"); $("select.month").append("&lt;option value=" + data[4].monthID + "&gt;" + data[4].month + "&lt;/option&gt;"); $("select.month").append("&lt;option value=" + data[5].monthID + "&gt;" + data[5].month + "&lt;/option&gt;"); $("select.month").append("&lt;option value=" + data[6].monthID + "&gt;" + data[6].month + "&lt;/option&gt;"); $("select.month").append("&lt;option value=" + data[7].monthID + "&gt;" + data[7].month + "&lt;/option&gt;"); $("select.month").append("&lt;option value=" + data[8].monthID + "&gt;" + data[8].month + "&lt;/option&gt;"); $("select.month").append("&lt;option value=" + data[9].monthID + "&gt;" + data[9].month + "&lt;/option&gt;"); $("select.month").append("&lt;option value=" + data[10].monthID + "&gt;" + data[10].month + "&lt;/option&gt;"); $("select.month").append("&lt;option value=" + data[11].monthID + "&gt;" + data[11].month + "&lt;/option&gt;"); }); </code></pre> <p>my json output looks like this:</p> <pre><code>[{"monthID":"1","month":"January"},{"monthID":"2","month":"February"},{"monthID":"3","month":"March"},{"monthID":"4","month":"April"},{"monthID":"5","month":"May"},{"monthID":"6","month":"June"},{"monthID":"7","month":"July"},{"monthID":"8","month":"August"},{"monthID":"9","month":"Septemeber"},{"monthID":"10","month":"October"},{"monthID":"11","month":"November"},{"monthID":"12","month":"December"}] </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