admin管理员组

文章数量:1323716

i have a little problem...i read a lot posts and i find some answers but...!i have multiple forms who created dynamically in PHP code!I have checkbox to every form and i check the forms to save the edits!my problem is when i check 3 forms only the first saved because only this post to save.php page!i make some changes to my code:

What i can do to make this code to send all forms to save.php page when i press th button save all

function save_all()
{
var all_elem=document.getElementsByTagName("*");
var same_id=new Array();
var index=0;
    var answer=confirm("The prices from checked products will change!Are you    sure");
    if(answer)
    {
        for(var i=0;all_elem.length;i++)
        {
            if(all_elem[i].id=="form1"){
            same_id[index++]=all_elem[i];
            document.forms[all_elem[i]].action="save_all.php";//error
            document.forms[all_elem[i]].submit();//error
            }
        }
    }


//this is my form and i have while loop to create multiple forms
<form id="form1" name="form[<?=$form_counter++;?>]" method="post" class="checkboxes">

i have a little problem...i read a lot posts and i find some answers but...!i have multiple forms who created dynamically in PHP code!I have checkbox to every form and i check the forms to save the edits!my problem is when i check 3 forms only the first saved because only this post to save.php page!i make some changes to my code:

What i can do to make this code to send all forms to save.php page when i press th button save all

function save_all()
{
var all_elem=document.getElementsByTagName("*");
var same_id=new Array();
var index=0;
    var answer=confirm("The prices from checked products will change!Are you    sure");
    if(answer)
    {
        for(var i=0;all_elem.length;i++)
        {
            if(all_elem[i].id=="form1"){
            same_id[index++]=all_elem[i];
            document.forms[all_elem[i]].action="save_all.php";//error
            document.forms[all_elem[i]].submit();//error
            }
        }
    }


//this is my form and i have while loop to create multiple forms
<form id="form1" name="form[<?=$form_counter++;?>]" method="post" class="checkboxes">
Share Improve this question edited Dec 22, 2011 at 7:47 tasox asked Dec 22, 2011 at 7:34 tasoxtasox 211 silver badge3 bronze badges 2
  • 2 Multiple elements should not have the same ID. – Tim M. Commented Dec 22, 2011 at 7:37
  • 1 Ditto - Have you got the message – Ed Heal Commented Dec 22, 2011 at 7:58
Add a ment  | 

2 Answers 2

Reset to default 5

First, IDs must be unique. Period.

http://www.w3/TR/html4/struct/global.html#adef-id

Secondly, that's way too much code and very inefficient DOM traversal.

var forms = document.getElementsByTagName("form");
for(var i = 0; i < forms.length; i++)
{
    forms[i].submit();
}

But I doubt this will work. A form submittal reloads the page in some fashion. How can multiple form submissions occur at once (other than in the extremely obscure case of submitting multiple forms to different windows)?

And if they all must occur at once, why aren't they in a single form?

First. Use unique IDs always

Second. You can send the forms one by one, using Ajax, with jQuery or other library you like. But better, use a single form, adding all your input fields to it. So, when you submit your form, all data will be sent to your PHP script (hint: use unique names for each of the fields, so you can identify which product they represent)

本文标签: POST Multiple forms with same id from javascriptStack Overflow