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.
galic1987Asked:
Who is Participating?
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.