Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Removing duplicatation of code within multiple <select> statements

  1. #1
    Join Date
    Oct 2005
    Location
    Liverpool, UK
    Posts
    87
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Removing duplicatation of code within multiple <select> statements

    Within a particular FORM, I have the need to repeat the same <select> statement (giving the same option values) a number of times.

    My values are different, but - for example:
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>

    This list is growing daily - which means I have to maintain several instances of it within the code. Is there a way I can set things up to just have the one list of <option values> that can be referred to within each <select> statement - allowing me to just maintain the one list within the code?

    Thanks in advance,

    N.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I don't know of anythign that will do this for you in pure html, and I think JS could do it, but I'd suggest PHP:

    Put this at the top of your page (before <html>):
    PHP Code:
    <?php
    $options 
    '<option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>'
    ;
    ?>
    Then code your html like normal, but use this for the selects:
    PHP Code:
    <select><?php echo $options?></select>
    Actually, you might just want to store the select as part of the variable in the php, then maybe call the var "$select"...

    Anyway, that should work.


    notes:
    1. to use php you must
    a. have php installed on your server.
    b. have your page(s) end with .php, not .htm. They'll work the same way, but just add .php functionality.
    2. more info on php at php.net

  3. #3
    Join Date
    Oct 2005
    Location
    Liverpool, UK
    Posts
    87
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question

    Many thanks for the response - I appreciate your time on this!!
    Unfortunately, I cannot use .php on the server in question....
    Anybody, got any ideas on how I could do this any other way .... js or other?

    Thanks again,

    N.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No.
    In HTML it isn't possible. In Javascript it is possible, but would be a very bad idea -- a form element is something you most definitely won't want to disallow non-JS users access to.
    As djr33 said, the only plausible way to do it is server-side.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Oct 2005
    Location
    Liverpool, UK
    Posts
    87
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question

    I understand where you're coming from and, to be honest, I hadn't thought of that scenario!

    However, the site requires the users to be JS 'enabled' for other elements - so I am confident that this shouldn't be an issue.

    I am interested in understanding how this could be delivered in JS if you could please elaborate?

    Thanks again....or, rather "domo arigato!"

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    However, the site requires the users to be JS 'enabled' for other elements
    Then I hope you go about rectifying this serious problem ASAP. Except in special cases (like a Javascript show-site), your site should never be dependent on Javascript.

    For the interim, however, you could use:
    Code:
    <script type="text/javascript">
      var options = new Array("a", "b", "c", "d"),
        values = new Array("a", "b", "c", "d"),
        optionslist = "<select>";
      for(var i=0;i<options.length;i++)
        optionslist += '<option value="' + values[i] + '">' + options[i] + '</option>';
      optionslist += "</select>";
    </script>
    Then:
    Code:
    <script type="text/javascript">
      document.write(optionslist);
    </script>
    Thanks again....or, rather "d&#244;mo arigat&#244;!"
    Heh... took up Japanese the week before last, just because I had some spare time on my hands
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Oct 2005
    Location
    Liverpool, UK
    Posts
    87
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thanks for that! I'll give it a whirl....
    I'm limited here in what I can do - basically HTML & JS seem to be the only tools I have to play with!

    I'd like to get a better understanding on .php, .asp and the like - but am limited with both systems and time.

    Stick with the Japanese - it's relatively "easy" to pick up the spoken word ... but forget about writing it in a hurry!!! I used to study it years ago whilst doing karate.... forgotten most of it by now.. but your note brought some of it back!

    Cheers,

    N.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Stick with the Japanese - it's relatively "easy" to pick up the spoken word ... but forget about writing it in a hurry!!!
    So I hear... I've got hiragana down, and I'm starting on memorizing the katakana, but kanji look rather formidable. I think it'll be a "five a day for the next year and a bit" job.
    I'd like to get a better understanding on .php, .asp and the like - but am limited with both systems and time.
    PHP is a much better idea than ASP. ASP is relatively unsupported, closed-source, platform-dependent, and just generally a nasty, ugly language. Not that PHP is amazing either, but it's certainly an improvement. BASIC was never meant for complex CGI scripts.
    How much are you paying? I've signed up with Inspire Networks, which are amazingly cheap and offer both PHP and JSP.
    Last edited by Twey; 04-20-2006 at 04:59 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Oct 2005
    Location
    Liverpool, UK
    Posts
    87
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    That sounds fairly heavy duty - good luck!!! You must have a lot of time on your hands!!

    I checked out Inspire and they are really cheap! Canadian-based though...?
    Are you over there or in the UK / other? Would it be feasible for me to have my site hosted in Canada with me being based in the UK I wonder? With broadband, does it really make any difference these days??

    I had been looking at 1and1.co.uk - but the basic Inspire package seems to be a lot better than their entry-level offering. At the moment I am just using some free space provided by Tiscali & the servers at work - hence the limitations. I know I need to get into php so I need to make a decision on the permanent hosting soon...

    Thanks again for the tips - I will be back again when I hit my next brick DHTML wall (i.e. "soon" !!)

    Cheers,

    N.

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I have real problems with 1and1. They annoy me in many ways.

    Look at godaddy.com (cheap, but horrible customer service), they're who I use.

    powweb.com and others are decent too.

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
  •