SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Could PHP be mucking with my JS?

    OK...here's my latest incarnation of my script. Essentially this adds an
    option to another select list after checking against a lookup array .

    My problem now is that the script only works peoperly on the first select
    list in the list. It "appears" to work with the others, however, it
    actually contains the data from the first list.

    The functions are called by the onchange handler for the associated
    select list, like so:

    HANDLER:
    Code:
    <select name="selIngredient[<?php echo $i ?>]" onChange="JavaScript: 
    addToSelect(<?php echo $arrName ?>, <?php echo $i  ?>);" size="1" id="<?
    php echo $i ?>">
    FUNCTION CODE
    Code:
    function addToSelect(arr, c)
    {
        var oOption;
        var len = document.forms[1].elements[0].options.length;
        var target = parseInt(document.forms[1].elements[0].options[c].index);
        alert(target);
     
        for(var t = 0; t < len;  t++)
        {
           
            if(document.forms[1].elements[0].options[t].selected == 
    true )        
            {            
                //alert('Option index is: ' + document.forms[1].elements
    [0].options[t].index );
                var text = document.forms[1].elements[0].options[t].value;
                text = text.split(":");
                //alert(text[1]);
                
                //alert('Option value is ' + document.forms[1].elements
    [0].options[t].value);
                 for(var j = 0; j < arr.length; ++j)
                    {
                        var lookFor = arr[j].split(":");
                        //alert('Looking for ' +  lookFor[0]);
                     if( text[1] == lookFor[0] )
                           {
                           alert(lookFor[0] + ':' + lookFor[1]);
                           alert('Found conversion  '  + lookFor[1] + '  for 
    ingredient ' + lookFor[0]);
                              oOption = new Option(lookFor[1], lookFor[1]); 
                              //alert(oOption);               
                              document.forms[1].elements[(target+2)].add
    (oOption);
                              alert(oOption.text + ' is the text\n' + 
    oOption.value + ' is the value.');
                          }
                     }    
                 } 
            } 
        }


  2. #2
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    haven't really looked at your code in detail, or even tried to figure out what it does, but one thing that strikes me as odd is that, in your function, you use document.forms[1].element[0] all the time...
    it's been a while since i looked at javascript, but that would suggest to me (and i might be wrong) that whatever you do in your function only affects the first element of your form, i.e. the first dropdown.
    if this is indeed the case, your function needs to be rewritten to have the form element as a passed-on variable of the function, and any manipulation should happen on the element that called the function...something like that anyway
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  3. #3
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The function checks a lookup array for a value, if it finds a value that corresponds with the selected item, then it adds an option to an associated drop-down box.

    As to your point, and I too have not looked at JS for some time, in this case, document.forms[1].elements[0] references the first select box in a row of inputs.

    [***edit***]
    referance like document.forms[0].elements[c,0]....where "c" is a counter variable works fine...
    [/***edit***]

    Now there can be any number of rows depending upon what the user provided, the first input in the elements array always being the drop-down box used for the main selecting.

    But this is somewhat Off-topic at this point as this is a PHP forum.

    I appreciate your observations though, as it was definitely a point to investigate.

    Too bad JS support for multidimensional arrays isn't anything like PHPs', it would make things much easier!

    Regards,

    Pete
    Last edited by phpPete; Sep 5, 2002 at 21:25.



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
  •