關於XMLHTTP和XML實現無刷新提交

關於XMLHTTP和XML實現無刷新提交


HTML頁面<scriptlanguage="javascript">functionchkform(){if(form1.UserName.value==""){alert("真實姓名不能為空!");form1.UserName.focus();returnfalse;}if(form1.Password.value==""){alert("電話號碼不能為空!");form1.Password.focus();returnfalse;}saveData();}functionsaveData(){varobjDom=newActiveXObject("MSXML2.DOMDocument");objDom.loadXML("<Root/>");oGeneral=objDom.createElement("username");oGeneral.text=form1.UserName.value;objDom.documentElement.appendChild(oGeneral);oGeneral=objDom.createElement("password");oGeneral.text=form1.Password.value;objDom.documentElement.appendChild(oGeneral);varxmlhttp=newActiveXObject("Microsoft.XMLHTTP");xmlhttp.open("post","submit\checklogin.asp",false);xmlhttp.send(objDom);if(xmlhttp.responseText=="True"){window.location="main.asp";}else{alert(unescape(xmlhttp.responseText));}}</script><formname="form1"method="post">用戶名稱:<inputname="UserName"type="text">用戶密碼:<inputname="Password"type="password"><imgsrc="3.jpg"width_="240"height="82"onClick="javascript:chkform()"style="cursor:hand"></from>submitchecklogin.asp頁面內容<!--#includefile="../include/const.asp"--><!--#includefile="../include/Function.asp"--><!--#includefile="../include/MD5.asp"--><%onerrorresumenext『------------------------------------------------------------------『---------------------讀取XML對象----------------------------------『------------------------------------------------------------------dimusername,passwordsetXmlDom=server.CreateObject("Msxml2.domdocument")XmlDom.async=FalseXmlDom.load(request)IfXmlDom.parseError.errorCode<>0Thenresponse.Write("Flase")response.End()EndIfusername=XmlDom.selectSingleNode("//username").textpassword=XmlDom.selectSingleNode("//password").textsetXmlDom=nothing『------------------------------------------------------------------『Response.Writeusername&password『Response.End()dimerrmsgsetrs=server.createobject("adodb.recordset")rs.ActiveConnection=strConnrs.Source="select*fromadminwhereusername=『"&username&"『"rs.CursorType=1rs.CursorLocation=2rs.LockType=3rs.open()ifrs.recordcount=1thenifrs.Fields.Item("password").Value=MD5(trim(password))thenifrs.Fields.Item("isclock").Value=1thenRandomize()Login_code=Hour(Now())&Minute(Now())&Second(Now())&Int(Rnd()*1000000)『Response.Write(Login_code)『Response.End()Response.Cookies(All_cookiesName)("ID")=rs.Fields.Item("cmp_id").ValueResponse.Cookies(All_cookiesName)("username")=rs.Fields.Item("username").ValueResponse.Cookies(All_cookiesName)("password")=rs.Fields.Item("password").ValueResponse.Cookies(All_cookiesName)("state")=rs.Fields.Item("state").ValueResponse.Cookies(All_cookiesName)("Login_code")=Login_codeifCookies_Expires<>0thenResponse.Cookies(All_cookiesName).Expires=Cookies_ExpiresendififCookies_Domain<>""thenResponse.Cookies(All_cookiesName).Domain=Cookies_Domainendifrs("Login_code")=Login_coders.update()rs.close()setrs=nothingResponse.Write("True")Response.End()elseResponse.Writeescape("您的用戶名已經被鎖定,
請向管理申請解鎖!")Response.End()endifelseResponse.Writeescape("您輸入的密碼錯誤!")Response.End()endifelseResponse.Writeescape("您輸入的用戶名不存在!")Response.End()endififerr.number<>0thenResponse.Writeescape("不知明錯誤!")%>這個是個最基本的,當然我們也可以根據特定的需要寫成公共函數,把所有的FORM表單里全部VALUE全部給生成XML對象,然後再對其操作。varyaya="";varcheckboxname="";varradioname="";varobjDom=newActiveXObject("MSXML2.DOMDocument");objDom.loadXML("<Root/>");for(vari=0;i<frm.elements.length;i++){if((frm.elements.type=="text")||(frm.elements.type=="hidden")){oGeneral=objDom.createElement(frm.elements.name);oGeneral.text=unescape(trim(frm.elements.value));objDom.documentElement.appendChild(oGeneral);}if(frm.elements.tagName=="SELECT"){oGeneral=objDom.createElement(frm.elements.name);oGeneral.text=unescape(frm.elements.options[frm.elements.selectedIndex].value);objDom.documentElement.appendChild(oGeneral);}if(frm.elements.tagName=="TEXTAREA"){oGeneral=objDom.createElement(frm.elements.name);oGeneral.text=unescape(trim(frm.elements.value));objDom.documentElement.appendChild(oGeneral);}if(frm.elements.type=="checkbox"){if(checkboxname!=frm.elements.name){for(varj=0;j<document.getElementsByName(frm.elements.name).length;j++){if(document.getElementsByName(frm.elements.name)[j].checked){yaya=yaya+document.getElementsByName(frm.elements.name)[j].value+",";}}yaya=yaya.substring(0,yaya.length-1);oGeneral=objDom.createElement(frm.elements.name);oGeneral.text=yaya;objDom.documentElement.appendChild(oGeneral);checkboxname=frm.elements.name;yaya="";}}if(frm.elements.type=="radio"){if(radioname!=frm.elements.name){for(varj=0;j<document.getElementsByName(frm.elements.name).length;j++){if(document.getElementsByName(frm.elements.name)[j].checked){yaya=document.getElementsByName(frm.elements.name)[j].value;break;}}oGeneral=objDom.createElement(frm.elements.name);oGeneral.text=yaya;objDom.documentElement.appendChild(oGeneral);radioname=frm.elements.name;yaya="";}}}alert(objDom.xml);varxmlhttp=newActiveXObject("Microsoft.XMLHTTP");xmlhttp.open("post",frm.action,false);xmlhttp.send(objDom);if(xmlhttp.responseText=="True"){//這裡就是操作成功}else{alert(unescape(xmlhttp.responseText));}frm為傳遞進來的form對象,這個需要放在一個函數里傳遞進去frm
推薦閱讀:

笑顏如花的美女+網路流行歌曲(刷新歌曲隨變)(美女美歌)
女人生孩子的最大年齡是?答案刷新你的三觀
90 分鐘粗鄙且猥褻的電視辯論,刷新了美國民主的下限
刷新櫥櫃細節 刷新入廚體驗(圖)

TAG:HTTP | 刷新 | 關於 | 實現 |