用於判斷使用者是否存在的ajax

來源:互聯網
上載者:User

註冊頁面的AJAX(javascript),功能用於判斷使用者是否存在,以使頁面從後台傳參數,而不會重新整理頁面。<script type="text/javascript">
         var http_request = false;
         var is_open=false;

           function callserver(){
          
            var user = document.getElementById("username").value;
             if ((user == null) || (user == "")) return false;
           
              http_request = false;

 //開始初始化XMLHttpRequest 對象
 if(window.XMLHttpRequest) { //Mozilla 瀏覽器
  http_request = new XMLHttpRequest();

  if (http_request.overrideMimeType) {//設定MiME 類別
   http_request.overrideMimeType("text/xml");
  }
 }else if (window.ActiveXObject) { // IE 瀏覽器
  try {
   http_request = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e){
   try {
    http_request = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e) {}
  }
 }

 if (!http_request) { // 異常,建立對象執行個體失敗
  window.alert("不能建立XMLHttpRequest 對象執行個體.");
  return false;
 }

 http_request.onreadystatechange = processRequest;

 // 確定發送請求的方式和URL 以及是否同步執行下段代碼
  var url="http://localhost:8086/bayestore/ajax.jsp?username="+user;
 http_request.open("GET",url, false);
 http_request.send(null);
          
        
            }

// 處理返回資訊的函數
function processRequest() {
 if (http_request.readyState == 4) { // 判斷對象狀態
  if (http_request.status == 200) { // 資訊已經成功返回,開始處理資訊
   document.getElementById("checkuser").value = http_request.responseText;
   alert(http_request.responseText);
  } else { //頁面不正常
   alert("您所請求的頁面有異常。");
  }
 }
}
  </script> 

註冊頁面的JSP表單標籤 

<form  action="register.do" name="UserRegister" method="POST">
         
            <TABLE width=750 border=0 cellPadding=3 cellSpacing=0 bgcolor="#F3F9FF">
            
              <TBODY>
              <TR>
                <TD colSpan=5 height=5></TD></TR>
              <TR>
                <TD width=60 height=25>
                  <DIV align=right>*</DIV></TD>
                <TD width=92 height=25>使用者名稱:</TD>
                <TD width=172 height=25><input type="text" name="username"></TD>
                <TD width=101 height=25><DIV align=center><input type="image"  src="image/buy_yanzheng.jpg" name="checkuser" id="checkuser" onclick="callserver();return false;"> </DIV></TD>
                <TD width=297 height=25>(驗證您的使用者名稱是否被佔用)</TD></TR>
              <TR>
                <TD height=25>
                  <DIV align=right>*</DIV></TD>
                <TD height=25>密 碼:</TD>
                <TD colSpan=3 height=25><input type="password" name="password">       &nbsp;(請設定6位以上密碼)              </TD></TR>
              <TR>
                <TD height=25>
                  <DIV align=right>*</DIV></TD>
                <TD height=25>確認密碼:</TD>
                <TD colSpan=3 height=25><input type="password" name="repassword">   &nbsp;(請再輸入一次密碼)              </TD></TR>
              <TR>
                <TD height=25>
                  <DIV align=right>*</DIV></TD>
                <TD height=25>使用者姓名:</TD>
                <TD colSpan=3 height=25><input type="text" name="zsname">                &nbsp;(請留下您的真實姓名,以便我們與您聯絡,為您發貨!) </TD></TR>
              <TR>
                <TD height=25>
                  <DIV align=right>*</DIV></TD>
                <TD height=25>性 別:</TD>
                <TD colSpan=3 height=25><SELECT class=buy_box name=UserSex>
                    <OPTION selected></OPTION> <OPTION value=male>男</OPTION>
                    <OPTION value=female>女</OPTION></SELECT> </TD></TR>
              <TR style="LINE-HEIGHT: 30px">
                <TD height=25>
                  <DIV align=right>*</DIV></TD>
                <TD height=25>聯絡電話:</TD>
                <TD colSpan=3 height=25><input type="text" name="telephone"> (請輸入區號與電話號碼)</TD>
              </TR>
              <TR>
                <TD height=25>
                  <DIV align=right>*</DIV></TD>
                <TD height=25>電子郵件:</TD>
                <TD colSpan=3 height=25><input type="text" name="email"> (請輸入電子郵件地址)</TD></TR>
              <TR>
                <TD height=25>
                  <DIV align=right>*</DIV></TD>
                <TD height=25>郵寄地址:</TD>
                <TD colSpan=3 height=25><TEXTAREA name="address" rows=3 cols=60></TEXTAREA></br>(請輸入正確的郵寄地址,以便準確及時將您訂購的商品送達)</TD></TR>
              <TR>
                <TD height=25>
                  <DIV align=right>*</DIV></TD>
                <TD height=25>郵遞區號:</TD>
                <TD colSpan=3 height=25><input type="text" name="zipcode" size="2" maxlength="6">(請輸入6位郵遞區號)</TD></TR>
              <TR>
                <TD height=25><DIV align=right>*</DIV></TD>
                <TD height=25>信用卡號:</TD>
                <TD colSpan=3 height=25><input type="text" name="idcard">
                (請輸入16位信用卡號) </TD>
              </TR>
              <TR>
                <TD height=25>&nbsp;</TD>
                <TD height=25>&nbsp;</TD>
                <TD colSpan=3 height=25>&nbsp;</TD></TR>
              <TR>
                <TD colSpan=2></TD></TR>
              <TR>
                <TD height=46>&nbsp;</TD>
                <TD colSpan=4><BR></TD>
              </TR>
              <TR>
                <TD height=46>
                  </TD>
                <TD colSpan=4>
                  <HR align=left width=600 SIZE=1>

                  <DIV align=center>
 <input type="Submit"  name="Submit" value=" 註冊 " onClick="checkUserReg('add');return false;"/>
                     <input type="reset" name="reset" value=" 重填 ">
                     <input name="action" type="hidden" value=""/>
                    </DIV></TD></TR>
              </TABLE></form>

AJAX發送到處理和響應的頁面:

<%@ page language="java" import="java.util.*"  pageEncoding="UTF-8"%>
<%@ page import="com.bay.bean.*" %>
<%
 String userName=request.getParameter("username");
 User user=new User();
    user.setUserCode(userName);
    if(user.ischeckreg())
    {out.write("恭喜,該使用者可以註冊使用");
    }else{out.write("對不起,該使用者名稱已經存在,重新輸入");}
 %>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.