views:

31

answers:

2

My page conisits of x amount of forms. One for each database entry. The user can change the data and save the individual item back to the database.

But where I am stuck is with a SAVE ALL button. Can you post multiple forms?? I am pretty certain you cannot with php, so I'm looking at javascript to solve my problems. Specifically:

document.forms.submit();

All that is going on is happening on the same page (i.e. when a form is submitted it puts a value at the end of the URL (foo?delete=true), and there is php at the top of the page which does something like: if delete is true -> delete field... else carry on as normal

My brain is saying I should do something like: (where $size is a count of how many forms there are)

<p onclick="saveAll('.$size.')">saveall</p>

and the javascript function:

function saveAll(size) {
    for(i=0;i<size;i++)
    {
         document.forms[i].submit();
         alert(i); // for testing purposes
    }
}

The result of this was that the page started to refresh, then a popup with "0" came up, then the page refeshed and nothing else happend.

Is what I need to do even possible? If so, hoooww?

Thanks

+1  A: 

I see 2 ways to escape this situation:

  • Put all your fields to one form
  • Submit your forms using AJAX and after last form submition is complete go to next page if needed.
Konstantin Likhter
I originally had all my fiels in 1 form but things got complex fast and i needed to have them in seperate forms (cant remember exactly why now)I think i will have to go for the ajax idea, thanks
Adam
A: 

But are all forms in the same page??? If yes I think the only way out is the ways suggested by Konstantin Likhter.

Otherwsie place each form in an IFRAME, then place all these IFRAMEs in the same main page, then using your code from the main page you can submit all the forms at once.

Marco Demajo
yeh all the forms are on one page >.<I personally hate iframes, so im gonna give ajax a whirl. thanks though
Adam