Reputation: 5270
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
Upvotes: 0
Views: 198
Reputation: 1039508
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
};
Upvotes: 8
Reputation: 2307
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
};
Upvotes: 1
Reputation: 40431
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
};
Upvotes: 1
Reputation: 26554
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;
}
Upvotes: 3
Reputation: 238115
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.
Upvotes: 3