Using IFRAME and Ajax without refreshing form submission

Source: Internet
Author: User
Tags php code

IFRAME implementation Ajax Way form submission

The code is as follows Copy Code

<script type= "Text/javascript" >
function Test (msg) {
Alert (msg);
}
</script>
<body>
<form action= "test.php" enctype= "Multipart/form-data" method= "post" target= ' Hidden_iframe ' >
<input type= "File" id= "file" name= "file"/>
<input type= "Submit" value= "submit"/></form><iframe name= "Hidden_iframe" id= "Hidden_iframe" Display:none; "></iframe>
</body>

test.php Code:

<?php
... code goes here ...
echo "<script type= ' text/javascript ' >parent.test (' some messages ');</script>";
?>

Ajax Submission Form

This gets all the contents of the form and then commits.

Form This parameter is the ID value of the form. Do not know how to direct the form object to pass in, and do not need to use getElementById to get.

Need to build a loading layer

The code is as follows Copy Code
function Ajaxsubmitform (form, resultdivid) {
Form=document.getelementbyid (form);
var elements = form.elements;//Enumeration The form elements
var element;
var i;
var postcontent = "";//Form contents need to submit
for (I=0;i<elements.length;++i) {
var element=elements[i];
if (element.type== "text" | | | element.type== "TEXTAREA" | | element.type== "hidden") {
Postcontent + = encodeURIComponent (element.name) + "=" + encodeURIComponent (element.value) + "&";
}
else if (element.type== "Select-one" | | element.type== "Select-multiple") {
var Options=element.options,j,item;
for (J=0;J&LT;OPTIONS.LENGTH;++J) {
ITEM=OPTIONS[J];
if (item.selected) {
Postcontent + = encodeURIComponent (element.name) + "=" + encodeURIComponent (item.value) + "&";
}
}
else if (element.type== "checkbox" | | element.type== "Radio") {
if (element.checked) {
Postcontent + = encodeURIComponent (element.name) + "=" + encodeURIComponent (element.value) + "&";
}
else if (element.type== "file") {
if (Element.value!= "") {
Postcontent + = encodeURIComponent (element.name) + "=" + encodeURIComponent (element.value) + "&";
}
} else {
Postcontent + = encodeURIComponent (element.name) + "=" + encodeURIComponent (element.value) + "&";
}
}
alert (postcontent);
Ajaxsubmit (Form.action, Form.method, postcontent);
}

function Ajaxsubmit (URL, method, Postcontent, Resultdivid) {
var loadingdiv = document.getElementById (' loading ');

Window.settimeout (function () {
Loadingdiv.innertext = "Loading."
LoadingDiv.style.display = "";
}, 1000);
Code for Mozilla, etc.
if (window. XMLHttpRequest) {
Xmlhttp=new XMLHttpRequest ();
}
Code for IE
else if (window. ActiveXObject) {
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}
if (XMLHTTP) {
Xmlhttp.onreadystatechange = function () {
If XMLHTTP shows "loaded"
if (xmlhttp.readystate==4) {
if (Resultdivid) {
document.getElementById (resultdivid). InnerHTML = Xmlhttp.responsetext;
}
else {
var result = document.createelement ("DIV");
Result.style.border= "1px solid #363636";
result.innerhtml = Xmlhttp.responsetext;
Document.body.appendChild (result);
}
loadingdiv.innerhtml = "Submit finnished!";
}
};
if (method.tolowercase () = = "Get") {
Xmlhttp.open ("get", url + "?" + Postcontent, True);
Xmlhttp.send (NULL);
}
else if (method.tolowercase () = = "Post") {
Xmlhttp.open ("POST", url, True);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send (postcontent);
}
} else {
loadingdiv.innerhtml = "Can" t create XMLHttpRequest object, please check your Web browser. ";
}
}

jquery Implementation Code

The code is as follows Copy Code

<script type = "Text/javascript" src = ". /js/jquery.js "></script>
<script type= "Text/javascript" >
function checkcorpid ()//Check if customer number is available
{
if ($.trim ("#txtF_CORPID") [0].value) = "")//txtf_corpid is the customer number input box
{
Alert ("Please enter customer number!");
}
Else
{
$ ("#checkFlag"). HTML ("detecting");//display of prompts
$.ajax ({
Type: "Get",
URL: "Checkcorpid.ashx",
Data: "Id=" +$.trim ($ ("#txtF_CORPID") [0].value),//Submit form, equivalent to Checkcorpid.ashx?id=xxx
Success:function (msg) {$ ("#checkFlag"). HTML (""); alert (msg); Operation after the successful operation! MSG is a backstage pass.
});
}
}
</script>

Background processing code


if (context. request.params["ID"]. ToString ()!= "")
{
Pxt.Logic.SYS.CORP_BASE_INFO Cbil = new Pxt.Logic.SYS.CORP_BASE_INFO ();
BOOL Flag=cbil.checkcorpid (context. request.params["ID"]. ToString ());
if (flag)
{
context. Response.Write ("The customer number is occupied!");
}
Else
{
context. Response.Write ("The customer number is available!");
}
}

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.