Release three Ajax-related functions, including refreshing new submission forms

Source: Internet
Author: User

A few months ago, due to project requirements, I wrote the following three Ajax-related functions. Release and share with you.
The first one is to load a segment of HTML without refreshing the content.
The second is to convert form data into a string of request strings.
The third is to combine functions I and II to implement the form without refreshing new submissions.

Another point to note is that you cannot process file uploads without refreshing new forms. This is mainly because of browser security settings. Currently, you can use IFRAME to upload a new image without refreshing it. We can find a lot in Google search.

Although there are already many excellent Ajax classes and functions on the Internet, some of my functions may be of some use to everyone, so I published them.
You can download it here.

CopyCode The Code is as follows: // @ DESC load a page (some HTML) via XMLHTTP, and display on a container
// @ Param URL the URL of the page will load, such as "index. php"
// @ Param request string to be sent, such as "Action = 1 & name = surfchen"
// @ Param method post or get
// @ Param container the container object, the loaded page will display in container. innerhtml
// @ Usage
// Ajaxloadpage ('index. php', 'Action = 1 & name = surfchen ', 'post', document. getelementbyid ('My _ home '))
// Suppose there is a HTML element of "my_home" ID, such as "<span id = 'my _ home'> </span>"
// @ Author surfchen <surfchen@gmail.com>
// @ URL http://www.surfchen.org/
// @ License http://www.gnu.org/licenses/gpl.html GPL
Function ajaxloadpage (URL, request, method, container)
{
Method = method. touppercase ();
VaR loading_msg = 'loading... '; // The text shows on the container on loading.
VaR loader = new XMLHttpRequest; // require cross-browser XMLHttpRequest
If (Method = 'get ')
{
URLs = URL. Split ("? ");
If (URLs [1] = ''| typeof URLs [1] = 'undefined ')
{
Url = URLs [0] + "? "+ Request;
}
Else
{
Url = URLs [0] + "? "+ URLs [1] +" & "+ request;
}

Request = NULL; // For get method, loader shocould send null
}
Loader. Open (method, URL, true );
If (Method = "Post ")
{
Loader. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
}
Loader. onreadystatechange = function (){
If (loader. readystate = 1)
{
Container. innerhtml = loading_msg;

}
If (loader. readystate = 4)
{
Container. innerhtml = loader. responsetext;
}
}
Loader. Send (request );
}
// @ DESC transform the elements of a form object and their values into request string (such as "Action = 1 & name = surfchen ")
// @ Param form_obj the form object
// @ Usage formtorequeststring (document. form1)
// @ Notice this function can not be used to upload a file. If there is a file input element, the func will take it as a text input.
// As I know, because of the security, in most of the browsers, we can not upload a file via XMLHTTP.
// A solution is IFRAME.
// @ Author surfchen <surfchen@gmail.com>
// @ URL http://www.surfchen.org/
// @ License http://www.gnu.org/licenses/gpl.html GPL
Function formtorequeststring (form_obj)
{
VaR QUERY_STRING = '';
VaR and = '';
// Alert (form_obj.length );
For (I = 0; I <form_obj.length; I ++)
{
E = form_obj [I];
If (E. Name! = '')
{
If (E. type = 'select-one ')
{
Element_value = E. Options [E. selectedindex]. value;
}
Else if (E. type = 'checkbox' | E. type = 'Radio ')
{
If (E. Checked = false)
{
Break;
}
Element_value = E. value;
}
Else
{
Element_value = E. value;
}
QUERY_STRING + = and + E. Name + '=' + element_value.replace (// \ &/g, "% 26 ");
And = "&"
}

}
Return QUERY_STRING;
}
// @ DESC no refresh submit (Ajax) by using ajaxloadpage and formtorequeststring
// @ Param form_obj the form object
// @ Param container the container object, the loaded page will display in container. innerhtml
// @ Usage ajaxformsubmit (document. form1, document. getelementbyid ('My _ home '))
// @ Author surfchen <surfchen@gmail.com>
// @ URL http://www.surfchen.org/
// @ License http://www.gnu.org/licenses/gpl.html GPL
Function ajaxformsubmit (form_obj, container)
{
Ajaxloadpage (form_obj.getattributenode ("action"). Value, formtorequeststring (form_obj), form_obj.method, container)
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.