0

I'm having a problem with my code... function retmsg isn't returning a value if javascript is implemented

<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>

value returned from retmsg() function should display in div id="error"

login script...

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 code...

$(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');
    }
}

in javascript code retmsg should return txt parameter to the att function..... att function is passing retmsg.txt value to the error1 function part of the function error1 is working as retmsg.txt is not returning value...

rest of the javascript code is working fine... rest of the code is same as of this... only function names are different....

ANSWER *ANSWER* ANSWER

javascript code was all correct made changes in the login script

$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));
}
user2881430
  • 367
  • 1
  • 5
  • 17
  • 4
    Please indent your code! It's currently hard to read. BTW, the MySQL extension is also deprecated and will be removed in the future. – ComFreek Oct 16 '13 at 15:01
  • 2
    Format you code properly if you expect humans to read them (yes, that includes yourself) – slebetman Oct 16 '13 at 15:02
  • 1
    Re your PHP `retmsg()` function: I strongly advise you to use PHP's built-in `json_encode()` function rather than building your Javascript object by hand like that. – Spudley Oct 16 '13 at 15:02
  • re ComFreek's comment: Please read [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for more info. – Spudley Oct 16 '13 at 15:04
  • Put `console.log(retmsg);` as the first line in the `success` function and see what value you're getting back. – Reinstate Monica Cellio Oct 16 '13 at 15:07
  • @Archer the function is returning value if i remove $('#login').submit(function(e) { att(); e.preventDefault(); }); – user2881430 Oct 16 '13 at 15:22
  • @Archer but returning that value on /login.php page – user2881430 Oct 16 '13 at 15:23
  • @Archer i think there is wrong in ajax call – user2881430 Oct 16 '13 at 15:27
  • use `json_encode` on an array for your `retmsg()` function. i.e. `return json_encode(array('status' => $status, 'txt' => $txt));` – Populus Oct 16 '13 at 15:47
  • @Populus, thanx for ur rply... but the problem is same again... ajax call in att() function is not working – user2881430 Oct 16 '13 at 15:58

0 Answers0