我尝试搜索,但没有任何答案像我想要的那样..好吧,我的问题。
我有一个名为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,其中DIV位于“index.php”文件中。在index.php中将显示何时使用ajax登录成功或失败时显示。但是我想从login.php页面调用这个DIV。
我可以这样做吗?谢谢
答案 0 :(得分:0)
检查一下。
$.post('index.php', $('form').serialize(), function(data){
$("#msg").html(data)
});
了解更多details
答案 1 :(得分: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);
}