Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I run a mysql query when the user selects an new option in a select field?
    primarykey
    data
    text
    <p>I want to have a select box that list categories of products. When a category is selected I want to simultaneously select the products in that category from the database. Do I need to use AJAX with this application? Any examples about doing this? Here is the code I'm working with:</p> <p>These functions build the options of each select field.</p> <pre><code>function buildCategoryOptions($catId = 0) { $sql = "SELECT cat_id, cat_parent_id, cat_name FROM tbl_category ORDER BY cat_id"; $result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error()); $categories = array(); while($row = dbFetchArray($result)) { list($id, $parentId, $name) = $row; if ($parentId == 0) { // we create a new array for each top level categories $categories[$id] = array('name' =&gt; $name, 'children' =&gt; array()); } else { // the child categories are put int the parent category's array $categories[$parentId]['children'][] = array('id' =&gt; $id, 'name' =&gt; $name); } } // build combo box options $list = ''; foreach ($categories as $key =&gt; $value) { $name = $value['name']; $children = $value['children']; $list .= "&lt;option value=\"$key\""; if ($key == $catId) { $list.= " selected"; } $list .= "&gt;$name&lt;/option&gt;\r\n"; foreach ($children as $child) { $list .= "&lt;option value=\"{$child['id']}\""; if ($child['id'] == $catId) { $list.= " selected"; } $list .= "&gt;&amp;nbsp;&amp;nbsp;{$child['name']}&lt;/option&gt;\r\n"; } } return $list; } </code></pre> <p>/* Build the product options list for the radio options */</p> <pre><code>function buildProductOptions($catId = 0) { $sql = "SELECT pd_id, pd_name, cat_id FROM tbl_product WHERE cat_id = $catId ORDER BY pd_name"; $result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error()); $numProduct = dbNumRows($result); $products = array(); while($row = dbFetchArray($result)) { list($id, $name) = $row; // we create a new array for each top level categories $products[$id] = array('name' =&gt; $name); } // build combo box options $list = ''; foreach ($products as $key =&gt; $value) { $name = $value['name']; $list .= "&lt;option value=\"$key\""; $list .= "&gt;$name&lt;/option&gt;\r\n"; } return $list; </code></pre> <p>} </p> <p>This is the page of the select fields:</p> <pre><code>$catId = (isset($_GET['catId']) &amp;&amp; $_GET['catId'] &gt; 0) ? $_GET['catId'] : 0; $categoryList = buildCategoryOptions($catId); $productList = buildProductOptions($catId); &lt;!--- Category Select ---&gt; &lt;select name="cboCategory" id="cboCategory" class="box"&gt; &lt;option value="" selected&gt;-- Choose Category --&lt;/option&gt; &lt;?php echo $categoryList; ?&gt; &lt;/select&gt; &lt;!--- Products Select : category is changed the products need to be from selected cat - --&gt; &lt;select name="selectOptions" id="selectOptions" class="box" multiple="multiple" &gt; &lt;option&gt;--Pick the other options--&lt;/option&gt; &lt;?php echo $productList; ?&gt; &lt;/select&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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