Tuesday, March 20, 2018

javascript - Why do my checkboxes disable other values?



I have multiple checkboxes that I'll be using and everytime I change the value of one of them, it makes the value of all the other ones false. The only thing I could think of is that when the value gets changed and sent to validate.php, it also looks for the other values and sets accordingly. If that is the case, how could I make it so that only the intended value gets changed.



JavaScript/JQuery Code Snippet (There is a change function for each variable):



$( document ).ready(function() {
var music = document.getElementsByName("music_status");
var music_value = document.getElementsByName("music_status")[0].value;

var rules = document.getElementsByName("rules_status");
var rules_value = document.getElementsByName("rules_status")[0].value;
var serverinfo = document.getElementsByName("serverinfo_status");
var serverinfo_value = document.getElementsByName("serverinfo_status")[0].value;
var module1 = document.getElementsByName("module1_status");
var module1_value = document.getElementsByName("module1_status")[0].value;
var module2 = document.getElementsByName("module2_status");
var module2_value = document.getElementsByName("module2_status")[0].value;
var module3 = document.getElementsByName("module3_status");
var module3_value = document.getElementsByName("module3_status")[0].value;


$(music).change(function() {
if ($(music_value).val() == 'false') {
$(music).val('true');
} else {
$(music).val('false');
}
window.location.href = 'validate.php?music_status=' + music_value;
})


$(rules).change(function() {
if ($(rules_value).val() == 'false') {
$(rules).val('true');
} else {
$(rules).val('false');
}
window.location.href = 'validate.php?rules_status=' + rules_value;
})

});



HTML Code Snippet (Have more of the checkboxes with the name and variable names changed):



 checked >



validate.php Code:




if (isset($_GET["music_status"]) && $_GET["music_status"] == 'false') {
$config -> SetVar("music_status", 'true', "Music Status");
} else {
$config -> SetVar("music_status", 'false', "Music Status");
}

if (isset($_GET["rules_status"]) && $_GET["rules_status"] == 'false') {
$config -> SetVar("rules_status", 'true', "Rule Status");
} else {
$config -> SetVar("rules_status", 'false', "Rule Status");

}

if (isset($_GET["serverinfo_status"]) && $_GET["serverinfo_status"] == 'false') {
$config -> SetVar("serverinfo_status", 'true', "Server Info Status");
} else {
$config -> SetVar("serverinfo_status", 'false', "Server Info Status");
}

if (isset($_GET["module1_status"]) && $_GET["module1_status"] == 'false') {
$config -> SetVar("module1_status", 'true', "Module 1 Status");

} else {
$config -> SetVar("module1_status", 'false', "Module 1 Status");
}

if (isset($_GET["module2_status"]) && $_GET["module2_status"] == 'false') {
$config -> SetVar("module2_status", 'true', "Module 2 Status");
} else {
$config -> SetVar("module2_status", 'false', "Module 2 Status");
}


if (isset($_GET["module3_status"]) && $_GET["module3_status"] == 'false') {
$config -> SetVar("module3_status", 'true', "Module 3 Status");
} else {
$config -> SetVar("module3_status", 'false', "Module 3 Status");
}

Answer



The problem seems to be



window.location.href = 'validate.php?music_status=' + music_value;



Note that each time you "refresh" the page you only send the status of the variable that changed over you could use:



$_SERVER['QUERY_STRING'] // contains the current _GET parameters


or an equivalent in javascript (See: how to get GET variable's value in javascript?) to append your new status to it.



Once you're done, your link should look something like this




window.location.href = 'validate.php?' + VALUES + '&music_status= + music_values 


where VALUES Contains all the previous values of the different variables (in the format of "var1=value&var2=value&var3=value"


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...