Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form action detection

Status
Not open for further replies.

XaceX

Programmer
Jul 31, 2007
3
0
0
US
Hello, I have tried to write a code to do this properly many times but continue to fail sometimes it works depending on the forms.

Basically I am looking to right a funtion that is run by any onclick placed in the button input of a form.

what i need it to do is onclick run thi function.

this function will read the form and take the action and ad the name and value of each input to it then display the final action.

this is what i have but cant seem to get it to add the entire thing together.

Code:
onclick=runme(this.form,action)
function runme(formz,actionz) {
   AllInputs = formz.getElementsByTagName("input");
   urlXX ='url'+actionz+'?';
 
      for (var i=0;i<AllInputs.length;i++) {
         InNmVl = AllInputs[i];
         
         AddIt = InNmVl.name + '=' + InNmVl.value + "&#38";
           if (InNmVl.type!="reset") {
             var UrlDisp=urlXX+AddIt; 
           } else { 
               return;  
           }
      }
}


can anyone please help?
 
Here is your code with some additions (in red) and an line deleted:
Code:
function runme(formz,actionz) {
   AllInputs = formz.getElementsByTagName("input");
   urlXX ='url'+actionz+'?';
    [!]var AddIt;[/!]
      for (var i=0;i<AllInputs.length;i++) {
         InNmVl = AllInputs[i];
         
           if (InNmVl.type!="reset") {
             AddIt [!]+[/!]= InNmVl.name + '=' + InNmVl.value + "&#38";
           } else { 
               return [!]urlXX+AddIt[/!];  
           }
      }
}
An alternative variant of this kind of function follows:
Code:
function runme(l_oForm, l_sAction) {
   var l_oInputs = l_oForm.getElementsByTagName("input");
   var l_sResult;
   for (var i=0, max=l_oInputs.length; i<max; i++) {
      var l_oInput = l_oInputs[i];
      if (l_oInput.type != "reset") {
         l_sResult += l_oInput.name + "=" + l_oInput.value + "&#38;";
      } else {
         return "url" + l_sAction + "?" + l_sResult;
      }
   }
}
This is still not ideal since you are always going to have a trailing ampersand - but it's a lot more logical in it's approach.

Note that this code will not handle checkboxes, radios or selects.

Let us know how you go!

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
How did this work out for you, XaceX? Did you find that it helped you?

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top