Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Im trying to return a Json array in php to jquery but keep receiving undifined. Im using a jquery Modal to load the login screen the user then logs in and if login is successful i want them redirected if not to display that actions. The code im using is

show: function (dialog) {
    $('#loginForm').on('submit', function (e) {
        e.preventDefault();
        $('input[type=submit]', this).attr('disabled', 'disabled');
        var username = $("#username").val();
        var password = $("#password").val();
        var url = "../_Scripts/login.php";
        if (!username) {
            $('input[type=submit]', this).removeAttr('disabled');
            $("#loginReply").html('<img src="../_Images/round_error.png" alt="Error" width="31" height="30" /> &nbsp; Please enter your Username.').show().fadeOut(6000);
            return false;
        } else if (!password) {
            $('input[type=submit]', this).removeAttr('disabled');
            $("#loginReply").html('<img src="../_Images/round_error.png" alt="Error" width="31" height="30" /> &nbsp; Please enter your Password.').show().fadeOut(6000);
            return false;
        } else {
            $.post(url, $('#loginForm').serialize(), function (data) {
                if (data.status == true) {
                    // window.location = data.action;
                    alert(data.status);
                } else {
                    //$("#loginReply").html(data.action).show().fadeOut(10000);
                    //$("#username").val('');
                    //$("#password").val(''); 
                    //$("#loginProcessGif").hide();
                    alert(data.status);
                }
            });
        }
    });
},

The Login php page looks like so

if($login){
        include("mysql-.php");
        $password2 = $password;                 
            if($login_query > 0) {
                while($row = mysql_fetch_array($sql)){
                    $idx = $row["id"];   
                    $user = $row["username"];                   
                    mysql_query("UPDATE members SET lastlogin=now(), mdhash='$logincode', ip='$ip' WHERE id='$idx'") or die(mysql_error());
            }

            return json_encode(array('status' => true, 'action' => '../home/'));
        }
        else {
            return json_encode(array('status' => true, 'action' => 'Username/Password incorrect'));
            exit();
        }
else{
return json_encode(array('status' => true, 'action' => 'Username/Password incorrect'));
                exit();
}

Can anyone help to why im getting undefined Vars

Thanks

share|improve this question
 
you can't just return a json encoded array in an if statement and expect it to go anywhere.... is all this code in some function you didn't show? Is there more? –  Digital Chris Dec 23 '13 at 19:30
 
No its not in a function. i originally used echo to return the data but i was struggling to refresh the page when the user logs in correctly and the page just loaded into the jquery window –  Benjio Dec 23 '13 at 19:33
 
similar: stackoverflow.com/questions/2410773/… –  Digital Chris Dec 23 '13 at 19:38
add comment

1 Answer

Sorted it. Found you can use

header('Content-Type: application/json');
            echo json_encode(array('status' => false, 'action' => 'Username/Password incorrect'));

to return a Json array :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.