1

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

<input type="checkbox" name="music_status" id="cmn-toggle-1" class="cmn-toggle cmn-toggle-round-flat" value="<?php echo $music_status?>" <?php if ($music_status == 'true') {?> checked <?}?>>
<label for="cmn-toggle-1"></label>

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");
}
Alpha Toon
  • 27
  • 4
  • 1
    You might look at checking the `checked` property of your checkboxes instead of the `value` property. – theGleep Oct 10 '17 at 16:43

1 Answers1

1

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"

Imme
  • 151
  • 3
  • I'm not really sure how I would go about using that. Would it be something along the lines of $vars = $_SERVER['QUERY_STRING']; parse_str($vars, $vars_arr); if (isset($vars["music_status"]) && $vars["music_status"] == false) { Code Here } – Alpha Toon Oct 10 '17 at 18:54
  • You'd use $_SERVER['QUERY_STRING'] in order to modify the redirection. The problem is that you're only passing a single value to the next page, you need to pass all of them. – Imme Oct 10 '17 at 19:12
  • I found my own method for having the other checkbox values not going to false by simply changing my else statements in the php code to else if and then simply changing == false to == true. I'll still accept this answer though as it'd be an alternate method. – Alpha Toon Oct 12 '17 at 03:21