Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 从其他PHP页面调用DIV_Jquery_Ajax_Html_Login - Fatal编程技术网

Jquery 从其他PHP页面调用DIV

Jquery 从其他PHP页面调用DIV,jquery,ajax,html,login,Jquery,Ajax,Html,Login,我试着搜索,但没有找到我想要的答案。。好的,这是我的问题 我有一个名为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">

我试着搜索,但没有找到我想要的答案。。好的,这是我的问题

我有一个名为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>
这里我的登录页面叫做

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>';
    }

}?>
检查一下这个

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

有关更多信息,您可以编辑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
?>

旁注:停止使用不推荐的
mysql.*
函数。请改用MySQLi或PDO。正如@Shivan Raptor所说,“停止使用mysql”,它已经被弃用,并且充满了威胁。。。。改用dbo或mysqli。
<?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
?>
$(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);
}