{if $member=="游客"}
<table width="210" height="208" border="0" cellpadding="0" cellspacing="0" background="images/shop_04.gif">
<form id="login" name="login" method="post" action="#" onsubmit="return lg(this)">
  <tr>
    <td width="64" height="35">&nbsp;</td>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td height="25" align="right">用户名:</td>
    <td colspan="2"><input name="name" type="text" id="name"  onmouseover="this.style.backgroundColor='#ffffff'" onmouseout="this.style.backgroundColor='#e8f4ff'" size="15" /></td>
  </tr>
  <tr>
    <td height="25" align="right">密码:</td>
    <td colspan="2"><input name="password" type="password" id="password"  onmouseover="this.style.backgroundColor='#ffffff'" onmouseout="this.style.backgroundColor='#e8f4ff'" size="15" /></td>
  </tr>
  <tr>
    <td height="25" align="right">验证码:</td>
    <td colspan="2"><input name="check" type="text" id="check"  onmouseover="this.style.backgroundColor='#ffffff'" onmouseout="this.style.backgroundColor='#e8f4ff'" size="10" /></td>
  </tr>
  <tr>
    <td height="30"><input name="check2" type="hidden" value="" /></td>
    <td width="44"><script>yzm(login);</script></td>
    <td width="80"><a onclick="javascript:code(login)" style=" cursor:hand">换一张</a></td>
  </tr>
  <tr>
    <td height="25" colspan="3" align="center"><input id="enter" name="enter" type="submit" value=""></td>
  </tr>
  <tr>
    <td colspan="3" align="center"><a href="#" id="login" onclick="reg()"><img src="images/check.JPG" width="59" height="23" border="0" /></a>&nbsp;<a id="login" href="#" onclick="found()"><img src="images/pass.JPG" width="59" height="23" border="0" /></a><strong></td>
    </tr>
  </form>
</table>
{else}
<table width="198" height="148" border="0" cellspacing="0" cellpadding="0" class="all">
<tr>
    <td align="center" valign="middle">欢迎您:{$member}</td>
</tr>
<tr>
    <td align="center" valign="middle"><a href="index.php?page_type=hyzx" class="lk">会员中心</a></td>
</tr>
<tr>
    <td align="center" valign="middle"><a href="index.php?page_type=shopcar" class="lk">查看购物车</a></td>
</tr>
<tr>
    <td align="center" valign="middle"><a onclick="javascript:logout()" style="cursor:hand" id="info">安全离开</a></td>
</tr>
</table>
{/if}这个页面是login.html登陆页面,onsubmit="return lg(this)内容提交给ajax.js代码。图如下:--------------------------------ajax.js内容-------------------------------// JavaScript Document
function lg(form){
if(form.name.value==""){
alert('请输入用户名');
form.name.focus();
return false;
}
if(form.password.value == "" || form.password.value.length < 6){
alert('请输入正确密码');
form.password.focus();
return false;
}
if(form.check.value == ""){
alert('请输入验证码');
form.check.focus();
return false;
}
if(form.check.value != form.check2.value){
form.check.select();
code(form);
return false;
}
var user = form.name.value;
var password = form.password.value;
var url = "chkname.php?user="+user+"&password="+password;
xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
var msg = xmlhttp.responseText;
if(msg == '1'){
alert('用户名或密码错误!!');
form.password.select();
form.check.value = '';
code(form);
return false;
}if(msg == "3"){
alert("该用户被冻结,请联系管理员");
return false;
}else{
alert('欢迎光临');
location.reload();
}
}
}
xmlhttp.send(null);
return false;
}
//显示验证码
function yzm(form){
var num1=Math.round(Math.random()*10000000);
var num=num1.toString().substr(0,4);
document.write("<img name=codeimg width=65 heigh=35 src='yzm.php?num="+num+"'>");
form.check2.value=num;
}
//刷新验证码
function code(form){
var num1=Math.round(Math.random()*10000000);
var num=num1.toString().substr(0,4);
document.codeimg.src="yzm.php?num="+num;
form.check2.value=num;
}
//注册
function reg(){
window.open("register.php", "_blank", "width=600,height=650",false);
}
//找回密码
function found() {
window.open("found.php","_blank","width=350 height=240",false);
}这个是ajax传递给chkname.php这个页面,传递方式GET。文件底层如图所示:---------------------------------------chkname.php--------------------
<?php
session_start();
header ( "Content-type: text/html; charset=UTF-8" );  //设置文件编码格式
require("system/system.inc.php");   //包含配置文件
$reback = '0';
$sql = "select * from tb_user where name='".$_GET['user']."'";
if(isset($_GET['password'])){
$sql .= " and password = '".md5($_GET['password'])."'";
}
$rst = $admindb->ExecSQL($sql,$conn);
if($rst){
/*  登录所用  */
if($rst[0]['isfreeze'] != 0){
$reback = '3';
}else{
$_SESSION['member'] = $rst[0]['name'];
$_SESSION['id'] = $rst[0]['id'];
$reback = '2';
}
}else{
$reback = '1';
}
echo $reback;
?>
可是在这里无法获取ajax.js传递的user值?我用的是smarty模板,不知道为什么?希望大家能多多指教和交流,谢谢!

解决方案 »

  1.   

    本帖最后由 xuzuning 于 2012-12-03 16:17:53 编辑
      

  2.   

    var msg = xmlhttp.responseText; 后面加上
    alert(msg); 什么也没有输出。
      

  3.   

    那表示 ajax 部分没有被执行
    离开框架,单独调试一下
      

  4.   


    //在Ajax的lg()函数中的23行后面,将user的值alert看一下是否为空,如果空,那就是参数form这个对象没有传对:
    var user = document.login.name;
    var pass = document.login.password;