ajax没有从php页面调用该函数

时间:2013-10-16 14:58:02

标签: javascript php jquery ajax function

我的代码出现问题... 如果实现了javascript,函数retmsg不返回值

<div id="pop">
    <a href="#" class="title">Elunika</a>
    <div class="txt">Its the way to connect with your friends.</div>
    <form action="login.php" method="post" id="login">
        <input id="email" placeholder="E-mail" type="text" name="em" />
        <input id="email" placeholder="Password" type="password" name="pwd"/>
        <div class="txt1"><input type="checkbox" name="check" />Keep me logged in |<a href="#"> Forgot Password ?</a></div>
        <input id="loginButton" type="submit" value="Login" name="log" />
    </form>
    <a href="#" id="reg"><span>Register</span></a>
    <div id="error1"></div>
    <div id="termdiv1"></div>
</div>
从retmsg()函数返回的

值应显示在div id =“error”

登录脚本......

if (isset($c))
{
    $q = mysql_query("select * from registeration where email='$a' and password='$b'");
    $r = mysql_num_rows($q);

    if($r)
    {
        $_SESSION["Authenticated"] = 1;
        $_SESSION['id'] = $a;
        echo retmsg(1,"profile.php");
    }
    else
    {
        $_SESSION["Authenticated"] = 0;
        die (retmsg(0,"Incorrect Information"));
    }
}
function retmsg($status,$txt)
{
    return '{"status":'.$status.',"txt":"'.$txt.'"}';
}

javascript代码......

$(document).ready(function(){
    $('#login').submit(function(e) {
        att();
        e.preventDefault();
    });
    $('#regForm').submit(function(e) {
        register();
        e.preventDefault();
    });
});


function register()
{
    hideshow('loading',1);
    error(0);

    $.ajax({
        type: "POST",
        url: "submit.php",
        data: $('#regForm').serialize(),
        dataType: "json",
        success: function(msg){
            if(parseInt(msg.status)==1)
            {
                window.location=msg.txt;
            }
            else if(parseInt(msg.status)==0)
            {
                error(1,msg.txt);
            }

            hideshow('loading',0);
        }
    });

}


function hideshow(el,act)
{
    if(act) $('#'+el).css('visibility','visible');
    else $('#'+el).css('visibility','hidden');
}


function error(act,txt)
{
    hideshow('error',act);
    if(txt) {
        $('#error').html(txt);
        $('#regpop').css('height','419px');
        $('#termdiv').css('margin-top','10px');
    }
    if(!txt) {
        $('#regpop').css('height','400px');
        $('#termdiv').css('margin-top','-20px');
    }
}


function att()
{
    hideshow1('loading',1);
    error1(0);
    $.ajax({
        type: "POST",
        url: "login.php",
        data: $('#login').serialize(),
        dataType: "json",
        success: function(retmsg){
            if(parseInt(retmsg.status)==1)
            {
                window.location=retmsg.txt;
            }
            else if(parseInt(retmsg.status)==0)
            {
                error1(1,retmsg.txt);
            }

            hideshow1('loading',0);
        }
    });
}


function hideshow1(el,act)
{   
    if(act) $('#'+el).css('visibility','visible');
    else $('#'+el).css('visibility','hidden');
} 


function error1(act,txt)
{
    hideshow1('error1',act);
    if(txt) {
        $('#error1').html(txt);
        $('#pop').css('height','290px');
        $('#termdiv1').css('margin-top','50px');
    }
    if(!txt) {
        $('#pop').css('height','270px');
        $('#termdiv1').css('margin-top','-35px');
    }
}

在javascript代码中 retmsg应该将txt参数返回到att函数..... att函数将retmsg.txt值传递给error1函数 函数error1的一部分正在工作,因为retmsg.txt没有返回值...

其余的javascript代码工作正常... 其余的代码与此相同...... 只有功能名称不同....

答案 * 答案 * 答案

javascript代码都是正确的 在登录脚本中进行了更改

$q = mysql_query("select * from registeration where email='$a' and password='$b'");
$r = mysql_num_rows($q);

if(!$r)
{
    $_SESSION["Authenticated"] = 0;
    die (retmsg(0,"Incorrect Information"));
}
else
{
     $_SESSION["Authenticated"] = 1;
     $_SESSION['id'] = $a;
     echo retmsg(1,"profile.php");
 }

function retmsg($status,$txt)
{
return json_encode(array('status' => $status, 'txt' => $txt));
}

0 个答案:

没有答案