4xjbh Posted August 16, 2013 Share Posted August 16, 2013 I am trying to update the second select box 'subcategory_id' but I don't know how to return the query values back to the control/twig template. All of the examples I have seen echo back html from a option while loop. Should I be echo back the html as per other common examples or is there a more twig compliant way to do this, maybe returning an array variable. Thanks in advance, James catalogues.php <script> $('#category_id').change( function() { checkCategory(); // checks value and enables/disables subcategory control var catid = $(this).val(); var dataString = 'catid='+catid; $.ajax ({ type: "POST", url: "catalogues.php", data: dataString, cache: false, success: function() { $("#subcategory_id")..........; // ????? } }) }); </script> <?php if(isset($_POST['catid'] )) { $catid=$_POST['catid']; $sql_subcategory = "SELECT catalogues_categories.id, catalogues_categories.category, catalogues_categories.parent_id, catalogues_categories.inuse FROM catalogues_categories WHERE catalogues_categories.parent_id = {$catid} AND catalogues_categories.inuse = 1 ORDER BY catalogues_categories.category ASC"; $query_subcategory = $conn->query($sql_subcategory); $results_subcategory = $query_subcategory->fetchAll(); } else { $results_subcategory = null; } ?> Link to comment https://forums.phpfreaks.com/topic/281227-return-array-results-to-dependent-select-boxes-in-twig-template/ Share on other sites More sharing options...
ignace Posted August 16, 2013 Share Posted August 16, 2013 The result you return is passed to the success function. success: function(data) { var el = document.getElementById('subcategory_id'); $(el).empty(); // remove all HTML inside <select/> $.each(data, function(i, option) { // add each <option/> from data $('<option value="' + option.id + '">' + option.name + '</option>').appendTo(el); }); } Link to comment https://forums.phpfreaks.com/topic/281227-return-array-results-to-dependent-select-boxes-in-twig-template/#findComment-1445282 Share on other sites More sharing options...
4xjbh Posted August 16, 2013 Author Share Posted August 16, 2013 Thanks but this returns a error in the js console (chrome) and the select control is empty. Uncaught TypeError: Cannot use 'in' operator to search for '3951' in <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"> </script> Link to comment https://forums.phpfreaks.com/topic/281227-return-array-results-to-dependent-select-boxes-in-twig-template/#findComment-1445284 Share on other sites More sharing options...
4xjbh Posted August 16, 2013 Author Share Posted August 16, 2013 Should line 32 be: ' + option.category + ' instead of ' + option.name + ' $('#category_id').change( function() { checkCategory(); // checks value and enables/disables subcategory control var catid = $(this).val(); var dataString = 'catid='+catid; $.ajax ({ type: "POST", url: "catalogues.php", data: dataString, cache: false, success: function(data) { var el = document.getElementById('subcategory_id'); $(el).empty(); // remove all HTML inside <select/> $.each(data, function(i, option) { // add each <option/> from data $('<option value="' + option.id + '">' + option.name + '</option>').appendTo(el); }); } }); }); Link to comment https://forums.phpfreaks.com/topic/281227-return-array-results-to-dependent-select-boxes-in-twig-template/#findComment-1445286 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.