2011-08-19 84 views
1

我只是在学习JQM,并从这个论坛有一些很好的指导。然而,让我感到困惑的是,我是否必须对php文件进行任何特殊标记。例如,我有一个html5文件,通过表单收集信息并使用ajax处理表单并发送到安全的php文件。出于某种原因,登录被拒绝或接受,但成功登录后不显示secure.php,它只停留在html页面。这只是我正在遵循的一个教程,但它让我有机会更详细地研究代码并尝试向前迈进。有人可以帮助这个吗?非常感谢是这个PHP正确的jQuery手机

<!DOCTYPE> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Logistor Login</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    <link href="http://code.jquery.com/mobile/latest/jquery.mobile.min.css" rel="stylesheet" type="text/css"/> 
    <script src="http://code.jquery.com/jquery-1.6.2.min.js" type="text/javascript"></script> 
    <script src="http://code.jquery.com/mobile/latest/jquery.mobile.min.js" type="text/javascript"></script> 
    <script> 


    $(function() 
    { 
     $("#login_form").submit(function() 
     { 
      //remove all the class add the messagebox classes and start fading   
      $("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000); 
      //check the username exists or not from ajax 
      $.post("ajax_login.php",{ user_name:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data) 
      { 
       if(data=='yes') //if correct login detail 
       { 
        $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox 
        { 
          //add message and change the class of the box and start fading 
         $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1, 
         function() 
         { 
           //redirect to secure page 
           document.location='secure.php'; 
         }); 
        }); 
       } 
       else 
       { 
        $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox 
        { 
         //add message and change the class of the box and start fading 
         $(this).html('Please provide the correct credentials').addClass('messageboxerror').fadeTo(900,1); 
        }); 
       } 
      }); 
      return false; //not to post the form physically 
     }); 
     //now call the ajax also focus move from 
     $("#password").blur(function() 
     { 
      $("#login_form").trigger('submit'); 
     }); 
    }); 
    </script> 
</head> 

<div data-role="page" id="login" data-title="Logistor Login" data-theme="b"> 
    <div data-role="header"> 
     <h1>Logistor Login</h1> 
    </div> 

    <div data-role="content"> 
     <form method="post" id="login_form" action=""> 
      <div data-role="fieldcontain"> 
       <label for="username">Username *</label> 
       <input type="text" name="username" id="username" value="" size="66" /> 
       <div id="userError"> 
       </div> 
      </div> 
      <div data-role="fieldcontain"> 
       <label for="password">Password *</label> 
       <input type="password" name="password" id="password" value="" size="46" /> 
       <div id="passError"> 
       </div> 
      </div> 
      <div id="login_message"></div> 
      <div id="submitDiv" data-role="fieldcontain"> 
       <input name="Submit" type="submit" id="submit" value="Login" data-inline="true" /> 
       <span id="msgbox" style="display:none"></span> 
      </div> 
     </form> 
    </div> 
    <div data-role="footer""> 
    <h4>Logistor 2009-2011</h4> 
</div> 
</div> 
</form> 

secure.php

<?php session_start(); 


// if session is not set redirect the user 
if(empty($_SESSION['u_name'])) 
    header("Location:index.html"); 

//if logout then destroy the session and redirect the user 
if(isset($_GET['logout'])) 
{ 
    session_destroy(); 
    header("Location:index.html"); 
} 

echo "<a href='secure.php?logout'><b>Logout<b></a>"; 
echo "<div align='center'>You Are inside secured Page</a>"; 

?> 

Ajax的login.php中

<?php session_start(); 


//Connect to database from here 
$link = mysql_connect('localhost', 'root', ''); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
//select the database | Change the name of database from here 
mysql_select_db('test'); 

//get the posted values 
$user_name=htmlspecialchars($_POST['user_name'],ENT_QUOTES); 
$pass=md5($_POST['password']); 

//now validating the username and password 
$sql="SELECT username_usr, password_usr FROM user_usr WHERE username_usr='".$user_name."'"; 
$result=mysql_query($sql); 
$row=mysql_fetch_array($result); 

//if username exists 
if(mysql_num_rows($result)>0) 
{ 
    //compare the password 
    if(strcmp($row['password_usr'],$pass)==0) 
    { 
     echo "yes"; 
     //now set the session from here if needed 
     $_SESSION['u_name']=$user_name; 
    } 
    else 
     echo "no"; 
} 
else 
    echo "no"; //Invalid Login 


?> 
+0

您需要显示ajax_login.php。看起来好像'u_name'会话变量没有被设置。 – JJJ

+0

@juhana我已经更新了原始代码。谢谢 – bollo

回答

1

在最低的限度,你是不是叫你在初始界定功能<script>。没有这些,任何事情都不应该发生

// add() right before the close of the <script> 
})(); 
</script> 
+0

除此之外,它现在没有任何区别,现在可以回到index.html。这是我得到脚本的地方,当我发布它时,它正在他的网站上工作。 http://roshanbh.com.np/2008/04/ajax-login-validation-php-jquery.html谢谢 – bollo

0

我知道这个问题是岁呢,你应该把你的SIAF(自调用匿名函数)在过去的文档中。这些函数在你的dom被加载之前开始执行。

<script> 
(function(){ 

})(); 
</script> 

可能在身体的前面或身体的尽头。