Closed Thread
Results 1 to 9 of 9

Thread: Ajax solution required - select onchange?

  1. #1
    norbie is offline Newbie
    Join Date
    Dec 2006
    Posts
    12
    Rep Power
    0

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    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.

  4. #3
    norbie is offline Newbie
    Join Date
    Dec 2006
    Posts
    12
    Rep Power
    0

    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!

  5. #4
    norbie is offline Newbie
    Join Date
    Dec 2006
    Posts
    12
    Rep Power
    0

    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.

  6. #5
    Feral is offline Programmer
    Join Date
    Jul 2008
    Posts
    163
    Rep Power
    15

    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.

  7. #6
    norbie is offline Newbie
    Join Date
    Dec 2006
    Posts
    12
    Rep Power
    0

    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!

  8. #7
    Feral is offline Programmer
    Join Date
    Jul 2008
    Posts
    163
    Rep Power
    15

    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)

  9. #8
    norbie is offline Newbie
    Join Date
    Dec 2006
    Posts
    12
    Rep Power
    0

    Re: Ajax solution required - select onchange?

    That works, thank you so much!

  10. #9
    Feral is offline Programmer
    Join Date
    Jul 2008
    Posts
    163
    Rep Power
    15

    Re: Ajax solution required - select onchange?

    No problem glad I could help

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Intermediate PHP, SQL, jQuery and AJAX - Populate Select Boxes
    By DEViANT in forum PHP Tutorials
    Replies: 6
    Last Post: 12-21-2011, 02:17 PM
  2. onchange to sql
    By chapm4 in forum JavaScript and CSS
    Replies: 1
    Last Post: 10-06-2011, 02:26 PM
  3. How to select data of a select list from database table
    By justsachin4u in forum PHP Development
    Replies: 26
    Last Post: 07-10-2011, 12:34 AM
  4. Select records older than 30 days (SOLUTION)
    By cdg10620 in forum Database & Database Programming
    Replies: 0
    Last Post: 03-30-2010, 10:42 AM
  5. onchange select statement within CF
    By jcschild in forum ASP, ASP.NET and Coldfusion
    Replies: 3
    Last Post: 11-13-2008, 10:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts