Web Analytics Made Easy -
StatCounter Inconsistant behavior from script - CodingForum

Announcement

Collapse
No announcement yet.

Inconsistant behavior from script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Inconsistant behavior from script

    Hi everyone,
    can anyone see whats wrong with this function?

    It copys the data to all form elements
    begining with "s" from form elements whose name is the same minus an s

    so "name" is copied to "sname"
    "address to "saddress" etc...

    its looks for the first character a position 5 because this form submits to PHP and the elements have names like "form[name]" rather than normal element names.

    about 1 in every 20 times it fails


    Code:
    function sameAs(thisform){
    		thisform = document.check1
    		for (n=10;n<thisform.elements.length;n++){
    			if (thisform.elements[n].name.charAt(5)=="s"){
    				if(thisform.elements[n].type=="text"){
    					halfstring=thisform.elements[n].name.substring(6,thisform.elements[n].name.length)
    					fullstring="form[" + halfstring
    					thisform.elements[n].value = thisform.elements[fullstring].value
    					}
    				else if(thisform.elements[n].type=="select-one"){
    					halfstring=thisform.elements[n].name.substring(6,thisform.elements[n].name.length)
    					fullstring="form[" + halfstring
    					thisform.elements[n].selectedIndex = thisform.elements[fullstring].selectedIndex
    					}
    			}
    		    }
    		}

    thanks for any help.

    Matt

  • #2
    Post your form as well so we can get a look.

    Comment


    • #3
      Hi

      I'm sorry to say that your question is not clear. Also, without the relastive html it is very hard to guess.
      What I can do, I can analyze your script with you and point out sensitive points so you can check whether the error might be that:


      function sameAs(thisform){
      thisform = document.check1

      /*you pass an ragument, then you reassign it: why? If it is an argument, it should already be referenced: sameAs(this.form) or sameAs(document.check1) . So question is: are you sure you meant to redraw the passed arguments? for if so, no reason to pass an ragument at all.*/

      for (n=10;n<thisform.elements.length;n++){

      /*n=10, correct? If a form has elements length==10, the loppo doesn't start, is this what you want?*/

      if (thisform.elements[n].name.charAt(5)=="s"){

      /*charAt(5) means on a name of 6 (SIX) chars, check the sixth: indexes start with 0: a possible issue here? I don't know.*/

      if(thisform.elements[n].type=="text"){
      halfstring=thisform.elements[n].name.substring(6,thisform.elements[n].name.length)

      /*substring 6? you sure? First index of substring method is inclusive: close range at beginning, openr ange at end: so if charAt(5)=="s" on "abcdes", substring(6) is undefined for last element is index 5. Possible issue here? */

      fullstring="form[" + halfstring

      /*what is that? what is it for? the returned string is:
      "form[anameMAYBE" with no closing square bracket. Also, the index might be undefined given the observation on substring(6)*/

      thisform.elements[n].value = thisform.elements[fullstring].value

      /*this apparently can't be: the reference is:
      thisform.elements[ "form[anameMAYBE" ].value
      still I see a missing bracket in the literal index (in itself legitimate)
      */

      }
      else if(thisform.elements[n].type=="select-one"){
      halfstring=thisform.elements[n].name.substring(6,thisform.elements[n].name.length)
      fullstring="form[" + halfstring
      thisform.elements[n].selectedIndex = thisform.elements[fullstring].selectedIndex

      /*same as above*/
      }
      }

      ;aybe I didn't udnerstand some part of your codes for I didn't understand well your question, but maybe these hints can point you in the right direction anyway.
      The two major issues I see: a literal which includes an opening bracket but I see no closing one.
      A charAt starting at 5 and a substring starting at 6, but what if moreover the length is 6: the higher index is 5.

      I hope this helps

      ciao
      }
      }
      Alberto http://www.unitedscripters.com/

      Comment


      • #4
        Hi,

        the form that uses this script is at:

        www.essentialoilsdirect.com/basket.php

        click on "go to checkout"

        (it doesn't matter that the basket's empty, but the page needs to be passed a basket to display the form.)

        Just to clear up your points:

        1)yes that argument bit is dodgy, I must have been asleep when i coded that, thanks for spotting it

        2)n=10 because I want the loop to ignore the first
        10 elements in the form, they are things like

        "form[street]"

        which my loop would try and fill with data from

        "form[treet]"

        If I let it act on those elements.

        3)I charAT(5) and Substring(6,... because I want to know if the first character after the "form[" bit is an "S"
        The Substring bit is reading after that character because that is what I use to differentiate the source fields from the ones I want to copy data into.

        Substring also pulls out the closing bracket, when it reads after the leading s it returns "name]"
        which my function bolts "form[" onto to make the real name of the element.

        The whole purpose of this script is to find an element begining with s (such as "form[sname]") and fill it with the data from the element that has the same name without the leading "s" (so in that case "form[name]"

        PS:The javascript is referenced from an external file, I would post that, but it is quite large. I am satisfied that everything else in it works OK and the problem is with this function.

        Thanks for all your help

        Matt

        Comment


        • #5
          Hi

          uhm, I am still scartching my head to make sure I understood ha ha.

          Ok, let's try this snippet:

          function sameAs(thisform){
          thisform = document.check1
          for (n=10;n<thisform.elements.length;n++){
          if (thisform.elements[n].name.charAt(5)=="s"){
          if(thisform.elements[n].type=="text"){
          halfstring=thisform.elements[n].name.substring(6,thisform.elements[n].name.length)
          fullstring="form[" + halfstring;

          /*BELOW: added ''code'' by me*/
          if(!confirm("can't find form field whose name is: "+fullstring)){return false}
          thisform.elements[n].value = thisform.elements[fullstring].value

          }
          else if(thisform.elements[n].type=="select-one"){
          halfstring=thisform.elements[n].name.substring(6,thisform.elements[n].name.length)
          fullstring="form[" + halfstring
          thisform.elements[n].selectedIndex = thisform.elements[fullstring].selectedIndex
          }
          }
          }
          }
          Apparently, the fields with the extracted name might not exist. is this hint useful?
          let us know

          ciao
          Alberto http://www.unitedscripters.com/

          Comment

          Working...
          X