2013-08-19 3 views
-1

Я пробую поиск, но ничего не отвечаю, как то, что я хочу. Хорошо, вот моя проблема.Звоните DIV Из другого PHP-страницы

У меня есть страница входа в индекс 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 " вызовите DIV с ID msg, где этот DIV в файле" index.php ". в index.php будет отображаться при появлении входа в систему с использованием ajax при успешном завершении или неудачной попытке. Но я хочу называть этот DIV с сайта login.php.

Могу ли я это сделать? Спасибо

+0

* Sidenote: * прекратить использование устаревших '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

Смежные вопросы