SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get values from selected dropdowns

    Hi there,

    I am currently developing a site which calculates prices based on selections made by the user. I am currently using
    Code:
    gvalue2[i] = p.elements['actor2_'+i].options[p.elements['actor2_'+i].selectedIndex].value;
    Now I realised that I will have to use arrays in PHP, so my select name changes from name="actor2_<?php echo $i; ?>" to name="actor2_[]".

    Now my JS code doesn't work anymore. How do I have have to change my code to make this work again? The i variable in the JS for statement start with i=0, so this should be OK.

    Any help is highly appriciated

    Regards ASchweti

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all, it always helps to store a reference, espcially for complicated ones. Not only can it aid code brevity, but it executes faster and increases legibility.

    var s = p.elements['actor2_[]'];
    gvalue2[i] = s.options[s.selectedIndex].value;
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Beetle,

    how come it's always you saving my life?

    Well, it's highly appriciated. Your answer solved my problem all the way

    Thanks and best regards,

    ASchweti

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm here to help
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Beetle,

    I suppose you have been in touch with a lot of stupid users until now and I have to admit, I am one of them

    I did the changes on our development server and tested on the live server...

    In the end your code didn't solve it (my face is turning red now).

    If you are still willing to give me hand, would you need more code?

    As I said I named the dropdown name="actor2_[]". We could get rid of the the underscore so. This would mean I end up with numerous dropdowns named actor2[].

    I am not sure if Javascript is now able to know which box I changed. Is it?

    This really has become an endless story for me

    Sorry for the confusion,

    regards ASchweti

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah yes, my fault, I wasn't complete. It depends on how many of these you have. Two or more creates an array, so you need to specify an index (the underscore is irrelevant)

    var s = p.elements['actor2_[]'][i ];

    That should
    do the trick!

    P.S. Ignore the space I put in, vB thinks it's an ITALIC vBCode tag [img]images/smilies/rolleyes.gif[/img]

    var s = p.elements['actor2_[]'].item( i );

    Might work as well
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Grrrr...

    Not meaning you! It just doesn't want to work

    I always get

    Error: 'p.elements[...].options' is NULL or not an object.

    Any more ideas. Would you like to see some more of my code?

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya - show me the HTML you're working with
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  9. #9
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Beetle,

    as I mentioned before I am one of the stupid ones

    I found the error. Yes this time I really did. I changed my for statement in the JS section
    Code:
    for (var i=0; i <= <?php echo $c_single+$c_adc-1; ?>; i++) {
    because I thought the index (i) has to start with 0 in order to work with the PHP array. I did not change that for statement in my PHP code as well

    Both of your inputs
    Code:
    var s = p.elements['actor2_[]'][];
    var s = p.elements['actor2_[]'].item(i);
    work fine.

    So let me send you a final thank you regarding this issue. I am really sorry for all the confusion.

    Regards ASchweti

  10. #10
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great! Glad you got it worked out!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •