Jump to content

AJAX chained select boxes with mysql php populate


samoht

Recommended Posts

Hello all,

 

I am trying to build a form that has two select boxes. The first select box is for the main category and the second is for the sub-category. I have set up the form to use ajax to populate the sub-category select box based on the selection of the category.

 

Problem is that right now I am just hard coding in the category in HTML and the sub category look up is just a php page that looks like this"

<?php

// List of Items for Income
if ($_GET['ch'] == 'Income') {
   ?>
<select name="itemList">
<option value="Wages">Wages & Tips</option>
<option value="Interest Income">Interest Income</option>
<option value="Dividens">Dividens</option>
<option value="Gifts Recieved">Gifts Recieved</option>
<option value="Refunds">Refunds</option>
<option value="Transfer From Savings">Transfer From Savings</option>
<option value="Other Income">Other</option>
</select>

<?php
}

// List of items for Home
if ($_GET['ch'] == 'Home Expenses') {
   ?>
<select name="itemList">
<option value="Mortgage/Rent">Mortgage/Rent</option>
<option value="Electricity">Electricity</option>
...

 

And the HTML form simply

    <label class="cf_label" style="width: 150px;">Expendature Type:</label>
    <select class="cf_inputbox" id="select_category" size="1" title=""  name="select_category"  onchange="htmlData('items.php', 'ch='+this.value)" >
    <option value="">Choose Option</option>
    <option value="Income">Income</option>
<option value="Home Expenses">Home Expenses</option>
<option value="Transportation">Transportation</option>
<option value="Health">Health</option>
<option value="Charity/Gifts">Charity/Gifts</option>
<option value="Daily Living">Daily Living</option>
<option value="Entertainment">Entertainment</option>
<option value="Savings">Savings</option>
<option value="Obligations">Obligations</option>
<option value="Subscriptions">Subscriptions</option>
<option value="Miscellaneous">Miscellaneous</option>
    </select>

    <label class="cf_label" style="width: 150px;">Item Type:</label>
<div id="txtResult">
    <select class="cf_inputbox" id="select_subcat" size="1" title=""  name="select_subcat">
    <option value="">Choose Option</option>
    </select>
</div>

 

The ajax simply replaces the contents of txtResult.

 

What I want to do is use mysql to populate both select boxes  - and to have the php code  so that I don't need to know the hard coded value of the category. (I assume that will have somthing to do with table relations?)

 

Any ideas??

 

Link to comment
Share on other sites

Just replace the contents of the if with a query to the database and build the subcategory menu with the results. There are millions of tutorials on how to get data from mysql for use in a dropdown.

 

You will use the value of $_GET['ch'] in the query you create to the database

Link to comment
Share on other sites

Thanks Matthew,

 

What I am not sure about is the query for two variables. I guess I can use a while or a for loop to filter out which category I would be searching for sub-categories too?

But even if I did that would I need a table relation in the sub-categories table with the categories table ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.