+ Reply to Thread
Results 1 to 9 of 9

Thread: Ajax solution required - select onchange?

  1. #1
    Newbie norbie is an unknown quantity at this point
    Join Date
    Dec 2006
    Posts
    12

    Ajax solution required - select onchange?

    Hi guys,

    I have a coding problem here that I can't quite figure out. For a clothing website I need to display stock information for each product related to colours and sizes available.

    I'm using a mysql database to store information on what sizes and colours are available. The structure for this is as follows:

    `prodcolours` table
    `colour_id`
    `colour_name` e.g. Brown

    `prodsizes` table
    `colour_id`
    `size` e.g. M
    `instock` (boolean)

    Might not be the easiest way to describe it, but every product has many colours. Each colour has a record stored in the database for S, M, L sizes and has a boolean field to determine whether or not it is in stock.

    On the product page, there is a drop down box for the user to select a colour. When the user selects a colour I need to show relevant radio buttons for the sizes available in that colour. Sizes that are not available show as greyed out (disabled="disabled"). This works absolutely fine when hardcoding the options for just 1 colour.

    The issue lies when the user selects a colour, I want the radio buttons to change to reflect the sizes available for the selected colours.

    The best way I thought of doing this is on page load, create a hidden div for each possible colour and fill them with the right radio buttons code. When the select drop down for colour is changed, make the correct div visible.

    e.g. colour1 is selected so make the colour1 div visible which contains the right size stock information.

    Does this make sense? I am very grateful for your help and suggestions.

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,751
    Blog Entries
    97

    Re: Ajax solution required - select onchange?

    I've actually used the hidden div method to accomplish a similar task (although I'm not sure why you would do this via AJAX and onload and not just directly with your server side language).

    I would, however, use a select onchange like your thread title states. When the user changes the the select option you could call a function which fetches all of the values from the database and populates another drop-down or radio buttons. This will cause the user some bit of lag (more for slow connections) but will reduce load and save bandwidth.

  3. #3
    Newbie norbie is an unknown quantity at this point
    Join Date
    Dec 2006
    Posts
    12

    Re: Ajax solution required - select onchange?

    Hi Jordan

    I really don't know a lot about ajax at all other than having used some code snippets before for the hidden div methods to "swap" a div.

    I don't even know what code to use to change a div with a select onchange, nevermind pulling data from the database at that time!

  4. #4
    Newbie norbie is an unknown quantity at this point
    Join Date
    Dec 2006
    Posts
    12

    Re: Ajax solution required - select onchange?

    I hope this explains it better, this is what I have at the moment:

    EDIT: I can't post links or images, how can I get round this? It would be much easier if you can see the picture.

    When a user selects a different colour from the drop down box, a new set of radio buttons must be loaded to represent the stock available in that colour. e.g. for the colour yellow, the "m" radio button is disabled as this product is out of stock in the colour + size.

    I thought the best way to do this would be to have the radio buttons within a div, and on page load create say 2 divs that are hidden, 1 for yellow, 1 for blue and when the user selects the relevant colour, un-hide the relevant one.

  5. #5
    Programmer Feral is on a distinguished road
    Join Date
    Jul 2008
    Posts
    162

    Re: Ajax solution required - select onchange?

    What you want to do is straight forward and only requires basic javascript. Ajax isn't required because you don't need to load any additional data because everything was loaded with the page but things are hidden.

    What you need to do is add an onChange event to the drop down menu that calls a function to hide the current color div and show the correct div for the selected color.

    Using a javascript library like jQuery would make this very easy for you and would only require about three lines of code.

  6. #6
    Newbie norbie is an unknown quantity at this point
    Join Date
    Dec 2006
    Posts
    12

    Re: Ajax solution required - select onchange?

    Hi Feral,

    I've been giving jquery a go and I hope you can help with the syntax I need to get this to work! So far I have been able to use the onchange event with jquery to show a hidden div. What I need to do now is specify to load a certain div, depending on which option has been selected from the select box.

    Here is the code I have now:

    Code:
      $(document).ready(function(){
        
        $("select").change(function () {
            $("tr#colYellow").show()
        })
    
      });
    HTML Code:
    <select name="product[]" class="categorymenu">      
    <option id="colYellow" value="Yellow">Yellow</option>
    <option id="colBlue" value="Blue">Blue</option>
    <option id="colWhite" value="White">White</option>
    </select>
    Code:
    <tr id="colYellow" style="display: none;">
    <td>stuff</td>
    </tr>
    Note: I had to use the TR element to show/hide rather than a DIV as this content is in the middle of a table, and show/hide doesnt work if you have table elements inside the div to be hidden!

    I hope you can help with this as I know that I am so close to getting it done!

  7. #7
    Programmer Feral is on a distinguished road
    Join Date
    Jul 2008
    Posts
    162

    Re: Ajax solution required - select onchange?

    Try something like this

    Code:
    <select name="product[]" class="categorymenu" id="colors">
    <option value="Yellow">Yellow</option>
    <option value="Blue">Blue</option>
    <option value="White">White</option>
    </select>
    Code:
    $(document).ready(function(){
      var col = col + $("#colors option:selected").val();
      $("#"+col).show();
    });
    Basically what I'm doing here is giving the select area the ID and using the jQuery form filters to get the value of the selected item then showing the corresponding div.

    (I wrote this off the top of my head so not sure if I made a mistake or not)

  8. #8
    Newbie norbie is an unknown quantity at this point
    Join Date
    Dec 2006
    Posts
    12

    Re: Ajax solution required - select onchange?

    That works, thank you so much!

  9. #9
    Programmer Feral is on a distinguished road
    Join Date
    Jul 2008
    Posts
    162

    Re: Ajax solution required - select onchange?

    No problem glad I could help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. SQL Functions - Math Functions
    By chili5 in forum Tutorials
    Replies: 6
    Last Post: 09-02-2009, 01:11 PM
  2. SQL: Select Queries
    By chili5 in forum Tutorials
    Replies: 3
    Last Post: 09-02-2009, 01:06 AM
  3. onchange select statement within CF
    By jcschild in forum ASP, ASP.NET and Coldfusion
    Replies: 3
    Last Post: 11-13-2008, 10:57 AM
  4. Considering Ajax, Part 1: Cut through the hype
    By Ronin in forum JavaScript and CSS
    Replies: 0
    Last Post: 12-13-2006, 12:36 PM
  5. AJAX Evolved
    By Jordan in forum JavaScript and CSS
    Replies: 0
    Last Post: 09-09-2006, 10:12 AM