Thursday, 9 August 2012

How to Submit Forms In back ground using javascript

When any user pays a HTML form is sent either by GET or Post request to the url Prossed in "ACTION" Type of Form


<FORM action="Link" method="post">
...form contents...
</FORM>
the example shown above shows that a HTTP POST request is published to the sendmail .php script on submission and you can also add the target="_blank" To the Form tag to the process the req in new window 
You have two options to submit a form on the page in the Background By not redirecting to another page 
First Option : You may create and undisplayed  IFRAME in your HTML page and save it as target to the  Original form This will allow you to submit the form  by realoding the parent window 
<FORM action="http://example.com/script.php"
           method="POST" target="hidden-form">
...form contents...
</FORM>
<IFRAME style="display:none" name="hidden-form"></IFRAME>  
The second Method in which you will be able make custom play loads before you submiting the form unlike the IFRAME which is based on submission The Code show below makes a standard form submit req By this your Location of the Browser changes and the page you are in will get added to the history of browser 
submitFORM('http://example.com/script.php', 'POST',
    {'name':'digital+inspiration', 'age':'100', 'sex','M'});
function submitFORM(path, params, method) {
    method = method || "post";
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);
    //Move the submit function to another variable
    //so that it doesn't get overwritten.
    form._submit_function_ = form.submit;
    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);
            form.appendChild(hiddenField);
         }
    }
    document.body.appendChild(form);
    form._submit_function_();
}

No comments:

Post a Comment