[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1038
  • Last Modified:

how can i collect all data from one form with javascript

how can i collect all data from one POST form with javascript by only knowing form name?
 just want to send post request thru ajax and get response.

but my problem is how to collect.
0
galic1987
Asked:
galic1987
  • 2
  • 2
1 Solution
 
devicCommented:
here an example:
===================================
<form name=myForm method="post" action="http://www.sembel.net/tools/q.php">
      <input name=city value=Athens>      
      <input name=country value=Greece>
      <input type="submit">
</form>

<script>
document.myForm.onsubmit = new Function("return smb()")
function smb()
{
      var els = document.myForm.elements;
      var str = "";
      for(var i=0; i<els.length; i++)
      {
            if(els[i].type != "submit")
            str += els[i].name + "=" + els[i].value;
            if(i < els.length-2)
                  str += "&";
      }
      alert(str);
      return false;
}
</script>
0
 
galic1987Author Commented:
great thank you, one question more:
how can I extend that with radio button?
0
 
devicCommented:
thanks, glad to help you

now with radio buttons:
==================================
<form name=myForm method="post" action="http://www.sembel.net/tools/q.php">
      <input type=text name=city value=Athens>      
      <input type=text name=country value=Greece>
      <input type=radio name=sex value=f>
      <input type=radio name=sex value=m>
      <input type="submit">
</form>

<script>
document.myForm.onsubmit = new Function("return smb()");
function smb()
{
      var els = document.myForm.elements;
      var str = "";
      for(var i=0; i<els.length; i++)
      {
            if(els[i].type != "submit")
            {
                  if(els[i].type == "radio")
                  {
                        if(els[i].checked)
                        {
                              if(i > 0)str += "&";
                              str += els[i].name + "=" + els[i].value;
                        }
                  }
                  else
                  {
                        if(i > 0)str += "&";
                        str += els[i].name + "=" + els[i].value;
                  }
            }
      }
      alert(str);
      return false;
}
</script>
0
 
galic1987Author Commented:
thank you very much!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now