0

i have this code

if($("input[name='staylogged']").is(":checked")){
        var staylogged = "yes";
        }else{
        var staylogged = "false";
        }

        var form_data = {
            username: $("#username").val(),
            password: $("#password").val(),
            staylogged
        };

but the var staylogged in form_data doesn't work as expected where is the error?

i don't know how to declare the var staylogged in form_data

1
  • Your variable staylogged is out of scope. Move its declaration ( var staylogged ) outside the if statement. Commented Oct 3, 2011 at 20:48

5 Answers 5

8

Don't redeclare the staylogged variable multiple times. A variable should be declared only once within a given scope. Also when building your form_data variable try to write valid javascript:

var staylogged = 'false';
if($('input[name="staylogged"]').is(':checked')) {
    staylogged = 'yes';
}

var form_data = {
    username: $('#username').val(),
    password: $('#password').val(),
    staylogged: staylogged
};

or a shorthand using the ? : operator:

var staylogged = $('input[name="staylogged"]').is(':checked') ? 'yes' : 'false';
var form_data = {
    username: $('#username').val(),
    password: $('#password').val(),
    staylogged: staylogged
};
Sign up to request clarification or add additional context in comments.

Comments

3

Your code inserts the value of staylogged where you've put it in the code. So your code basically reads like this:

var form_data =  {
    username: $("#username").val(),
    password: $("#password").val(),
    true
};

Which obviously doesn't make sense. You need to provide a key:

var form_data = {
    username: $("#username").val(),
    password: $("#password").val(),
    staylogged: staylogged
};

Object literals need key-value pairs. The name of the key isn't inferred from a variable name.

Comments

3
 var form_data = {
     username: $("#username").val(),
     password: $("#password").val(),
     staylogged: staylogged
 };

This will result in form_date.staylogged being yes or false

Also i would recommend you set staylogged to true and false, it's easier to validate.

if($("input[name='staylogged']").is(":checked")){
    var staylogged = true;
} else {
    var staylogged = false;
}

Comments

1

Your staylogged variable is out of scope. Try declaring var staylogged outside of your if statement. You also need to give it a key.

var staylogged = ""; // give it a default value
if($("input[name='staylogged']").is(":checked")){
    var staylogged = "yes";
} else {
    var staylogged = "false";
}

var form_data = {
    "username": $("#username").val(),
    "password": $("#password").val(),
    "staylogged": staylogged
};

Comments

1

Did you just want a field named staylogged that has the same value as the local one? If so, then you still just need to assign it like normal:

var form_data = {
    username: $("#username").val(),
    password: $("#password").val(),
    staylogged: staylogged
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.