2013-08-19 61 views
-1

我嘗試搜索,但沒有答案就像我想要的..好吧在這裏我的問題。從其他PHP呼叫DIV頁

我有一個名爲index.php的登錄頁面,裏面的index.php有登錄信息的div。

的index.php

<html> 

<head> 
<meta http-equiv="Content-Language" content="en-us"> 
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 

<script type="text/javascript" src="js/login.js"></script> 

<title></title> 
</head> 

<body> 

<form method="POST"> 
    <p>Username <input type="text" name="cUsername" size="20"></p> 
    <p>Password <input type="text" name="cPassword" size="20"></p> 
    <p><input type="submit" value="Login" name="B1"></p> 
</form> 

<div id="msg"></div> 
</body> 

</html> 

,在這裏我jQuery的,如果不刷新頁面登錄。

login.js

$(document).ready(function() { 
    $('#Loading4').hide();  
}); 

function ajax-login(){ 

    var cUusername = $("#cUsername").val(); 
    var password = $("#password").val(); 

    if(cUsername.length > 2){ 
     $('#Loading4').show(); 
     $.post("login.php", { 
      cUsername: $('#cUsername').val(), 
      password: $('#password').val(), 
     }, function(response){ 
      $('#Info4').fadeOut(); 
      $('#Loading4').hide(); 
      setTimeout("finishAjax4('Info4', '"+escape(response)+"')", 450); 
     }); 
     return false; 
    } 
} 

function finishAjax4(id, response){ 

    $('#'+id).html(unescape(response)); 
    $('#'+id).fadeIn(1000); 
} 

,在這裏我稱之爲登錄頁面

的login.php

<?php 

if($_REQUEST) 
{ 
    $username = $_REQUEST['username']; 
    $query = "select * from tbl_user where username = '".strtolower($username)."'"; 
    $results = mysql_query($query) or die('Error to connect to the database'); 

    if(mysql_num_rows(@$results) > 0) // not available 
    { 
     echo '<div id="msg">Login Successful !</div>'; 
    } 
    else 
    { 
     echo '<div id="msg">Not Register Yet !</div>'; 
    } 

}?> 

我的意思是,我想顯示消息的成功或失敗「 login.php「呼叫ID爲msg的DIV位於」index.php「文件中的DIV。在index.php中將顯示使用ajax登錄時成功或失敗時顯示。但是我想從login.php頁面調用這個DIV。

我可以這樣做嗎?謝謝

+0

*旁註: *停止使用不推薦的'mysql_ *'函數。改用MySQLi或PDO。 – Raptor

回答

0

檢查了這一點。

$.post('index.php', $('form').serialize(), function(data){ 
    $("#msg").html(data) 
});​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

更多details

+0

謝謝,我會盡快回復 –

0

您可以編輯您的login.php來響應,JSON

<?php 

if($_REQUEST) 
{ 
    $username = $_REQUEST['username']; 
    $query = "select * from tbl_user where username = '".strtolower($username)."'"; 
    $results = mysql_query($query) or die('Error to connect to the database'); 

    if(mysql_num_rows(@$results) > 0) // not available 
    { 
     $res = array('id'=>'msg', 'data'=>'Login Successful !');//here you can pass the id you need to change it and the new html data 
    } 
    else 
    { 
     $res = array('id'=>'msg', 'data'=>'Not Register Yet !');//here also you can pass the id you need to change it and the new html data 
    } 

} 
header('Content-type: application/json');// to response as JSON code in the header 
echo json_encode($res);// to encode the result string as JSON code 
?> 

而且login.js會是這樣的:

$(document).ready(function() { 
    $('#Loading4').hide();  
}); 

function ajax-login(){ 
    var cUusername = $("#cUsername").val(); 
    var password = $("#password").val(); 
    if(cUsername.length > 2){ 
     $('#Loading4').show(); 
     $.post("login.php", { 
      cUsername: $('#cUsername').val(), 
      password: $('#password').val(), 
     }, function(response){ 
      $('#Info4').fadeOut(); 
      $('#Loading4').hide(); 
      setTimeout(function(){ 
        finishAjax4(response['id'], response['data']) 
      },450); 
     }); 
     return false; 
    } 
} 

function finishAjax4(id, response){ 

    $('#'+id).html(unescape(response)); 
    $('#'+id).fadeIn(1000); 
} 
+0

謝謝,我會盡快回復 –

+0

,請作爲@Shivan Raptor說:「停止使用mysql」它已被棄用且充滿威脅....使用dbo或mysqli代替。 – MaveRick