CI中AJAX的使用

JS 复制代码
 
<script type = "text/javascript" >
var xmlobj ;  //定义XMLHttpRequest对象
function CreateXMLHttpRequest ( )
{
         if (window. XMLHttpRequest )
         { //Mozilla浏览器
                xmlobj = new XMLHttpRequest ( ) ;
                 if (xmlobj. overrideMimeType )
                 { //设置MIME类别
                        xmlobj. overrideMimeType ( "text/xml" ) ;
                 }
         }
         else  if (window. ActiveXObject )  {  //IE浏览器
                 try  {
                        xmlobj = new ActiveXObject ( "Msxml2.XMLHttp" ) ;
                 }
                 catch (e )  {
                         try  {
                                xmlobj = new ActiveXobject ( "Microsoft.XMLHttp" ) ;
                         }
                         catch (e )  {
                         }
                 }
         }
}
 
function Validate ( )  //主程序函数
{
        CreateXMLHttpRequest ( ) ;  //创建对象
         var showurl  =  "check_username?username="  + document. getElementById  ( "username" ). value ;  //构造URL
        xmlobj. open ( "GET" , showurl ,  true ) ;  //调用validate.phpmingm
        
        xmlobj. onreadystatechange  = StatHandler ;  //判断URL调用的状态值并处理
        xmlobj. send ( null ) ;  //设置为不发送给服务器任何数据
}
 
function StatHandler ( )  {  //用于处理状态的函数
         if (xmlobj. readyState  ==  4  && xmlobj. status  ==  200 )  //如果URL成功访问,则输出网页
         {
                document. getElementById ( "msg" ). innerHTML =xmlobj. responseText  ; //替换,即输出结果。
         }
}
</script >
 
复制代码


View
HTML 复制代码
 
<HTML>
<form action="<?php echo site_url('user/do_register')?>" name="form1" method="post" accept-charset="utf-8">
         <table border="0" cellspacing="1" cellpadding="0">
                 <tr>
                         <th><label for="username">登录帐户名: <em>* </em></label></th>
                         <td class="input"><input type="text" name="username" id="username"value="" class="text"></td>
                         <td id="msg"></td>
                 </tr>
        </table>
<form>
</HTML>
 
复制代码


controller
PHP 复制代码
 
<?php
function check_username ( )  {
parse_str ( $_SERVER [ 'QUERY_STRING' ] ,  $_GET ) ;    //因为CI默认不使用get方法,所以使用的时候要在前面加上这一句
$username  =  $_GET [ "username" ] ;
if ( $query  =  $this -> user_model -> check_username ( $username ) ) {
         echo  "<p style='color:ff0000'>该用户名已被注册</p>" ;
}
else  {
         echo  "<p style='color:green'>该用户名可以使用</p>" ;
}
}
?>
 
复制代码

对了,还要将config.php改一下
PHP 复制代码
 
<?php
$config [ 'uri_protocol' ]  =  "PATH_INFO" ;
?>
 
复制代码
发布了17 篇原创文章 · 获赞 1 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/a7282787/article/details/47439739