tags:

views:

58

answers:

3

Hello, I am writing a simple form submit function for a specific site. The script fills the specific form on the current page and submits the form. It MUST also fill the second form which appears after the first form is submited and submit this form too.

That is there are 2 forms that must be filled and submitted.

Problem however, is that on a normal page the script fills and 1st form and submits it. After submission however, the script stops working! I want to continue execution!

Ive done it by mistake on a page that had 2 frames! most pages dont have frames!

any help????? i am stuck.

function FillFirstForm(){

doc=document;
//Some code removed from above...Fill Values
doc.forms[0].name.value = answerarray[1];
doc.forms[0].address.value = answerarray[2];
doc.forms[0].phone.value = answerarray[3];
doc.forms[0].username.value = answerarray[4];

//And Press Button
doc.forms[0].submit.click();

iTimer=setInterval(FillSecondForm,5000);
return true;

}

function FillSecondForm(){
clearInterval(iTimer);
doc.forms[0].tags.value = answerarray[5];
doc.forms[0].reference.value = answerarray[6];
document.forms[0].submit.click();
return true;

}

A: 

The basic rule is that a classical form submission halts all execution of scripts of the page, and loads the next page. No way to do anything about that.

You may have to switch to sending your form's contents using Ajax. That will leave the current page alive, and allow you to do as many submits as you want to.

A very easy way to achieve this is the jQuery form plugin. It takes much of the manual hassle out of the process.

Pekka
Unfortunatly i dont have access to page code. I am only writing the script to fill the forms.
Ray
@Ray then I'm afraid I think it is not possible. You have to do an Ajax submit or submit into an iframe to keep the sending page intact. You would have to be able to manipulate the DOM using your script - you could then add that functionality. If you can't alter the form at all, I think it can't be done.
Pekka
xm, how can i manipulate DOM and add functionality? Note that Javascript is not my strong point!
Ray
@Ray this should be possible by loading the jQuery form plugin. I think tt doesn't require changes in the DOM, just load it and initialize it.
Pekka
emm, litle help?<form id="basic_form" name="basic" //..<td rowspan="2"><input class="submit" name="submit" type="submit" value="confirm" style="font-size: 10pt;" /></td><td rowspan="2"><input class="update" name="update" type="submit" value="update set" style="font-size: 10pt;" /></td></tr></table></form>how to submit(not update) using ajax?
Ray
A: 

Can you call FillSecondForm() function on body load of second form, once first form submitted.

Jprogyog
to tell you the truth, i dont know! can i? as i said i dont know much javascript..I am just writing an addon for firefox to do this filling automatically.
Ray
A: 

You could POST the first form with AJAX, and then submit the second form after receving the response for the first form.

Something like this perhaps:

function FillFirstForm() {

    var post_data = 'name='+escape(answerarray[1]);
    post_data += '&address='+escape(answerarray[2]);
    post_data += '&phone='+escape(answerarray[3]);
    post_data += '&username='+escape(answerarray[4]);

    // TODO: make this cross browser :)
    var req = new XMLHttpRequest();

    req.onreadystatechange = function() {
        if ( req.readyState == 4 ) { // The request has finished
            if ( req.status == 200 ) {
                FillSecondForm();
            }
            else {
                // Deal with errors here
            }
        }
    };

    req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    req.open('POST', '/submit_url', true);
    req.send(post_data);
}

W3 schools has a tutorial on AJAX here: http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp, and there are many more on the web.

tomit
nice!!This i can undertand! ok. done it. However, the first form is submited by ajax. it works. But now, FillSecondForm() does not seem to work. the RESPONSE is in the memory somewhere i guess! how will i go about filling and submiting the second form!?!?
Ray
The response is stored by the XMLHttpRequest, i.e. the req variable. You can access it via req.responseText or req.responseXML (use the latter only if it's a well-formed XML document). FillSecondForm() should work in the same way it did before FillFirstForm() was refactored. I would recommend checking your JavaScript console error log (Ctrl+Shift+J in Firefox) to look for messages detailing why FillSecondForm() failed. You also could refactor FillSecondForm() to use AJAX too, but note that it won't cause the page to reload, which may not be what you want.
tomit
i included some exception handling and the error is that document.forms[0].submit.click(); is undefined. I removed all the other commands for testing. I just left the click command.i think i am missing something...
Ray
Try just document.forms[0].submit(). Although, given that the function name is FillSecondForm, perhaps it should be document.forms[1].submit()? Either way, the Form object has a submit() function which should submit it.
tomit
No. it does not work.The function "FillSecondForm" 'sees' the first form only! the one that i have on me screen.maybe i missed something?
Ray