oz11 Posted September 14, 2023 Share Posted September 14, 2023 (edited) <?php if($_GET['sfw'] == "true"){ $sfw = 1; setcookie("nsfw", "true"); $_COOKIE["nsfw"] = "true"; } if($_GET['sfw'] == "false"){ $sfw = 0; setcookie("nsfw", "false"); $_COOKIE["nsfw"] = "false"; } if(!isset( $_COOKIE["nsfw"])){ $sfw = 0; setcookie("nsfw", "false"); $_COOKIE["nsfw"] = "false"; } ?> <?php if($_COOKIE["nsfw"] == "true"){ ?> <input type="checkbox" id="sfw" name="sfw" value="false" checked="checked">Filter NSFW? <?php } elseif($_COOKIE["nsfw"] == "false") { ?> <input type="checkbox" id="sfw" name="sfw" value="true" >Filter NSFW? <?php } else { ?> <input type="checkbox" id="sfw" name="sfw" value="true" >Filter NSFW? <?php } What am I doing wrong freaks? Can you spot anything? Ta. ps: it turns on and off but not on again.. Edit: i mean it turns on bt not off Edited September 14, 2023 by oz11 Quote Link to comment https://forums.phpfreaks.com/topic/317286-nsfw-filter-conditionals/ Share on other sites More sharing options...
requinix Posted September 14, 2023 Share Posted September 14, 2023 Checkboxes are only submitted in form data when they are checked. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317286-nsfw-filter-conditionals/#findComment-1611831 Share on other sites More sharing options...
oz11 Posted September 14, 2023 Author Share Posted September 14, 2023 (edited) 5 minutes ago, requinix said: Checkboxes are only submitted in form data when they are checked. Thanks. Is there a workaround? Ps: Glad you said that, I thought I was ging mad (again). Edited September 14, 2023 by oz11 Quote Link to comment https://forums.phpfreaks.com/topic/317286-nsfw-filter-conditionals/#findComment-1611832 Share on other sites More sharing options...
oz11 Posted September 14, 2023 Author Share Posted September 14, 2023 <?php if($_GET['nsfw_options'] == "true"){ $sfw = 1; setcookie("nsfw", "true"); $_COOKIE["nsfw"] = "true"; } if($_GET['nsfw_options'] == "false"){ $sfw = 0; setcookie("nsfw", "false"); $_COOKIE["nsfw"] = "false"; } if(!isset( $_COOKIE["nsfw"])){ $sfw = 0; setcookie("nsfw", "false"); $_COOKIE["nsfw"] = "false"; } ?> NSFW Filter: <?php if($_COOKIE["nsfw"] == "true"){ ?> <select name="nsfw_options" id="nsfw_options"> <option value="true">Enable</option> <option value="false">Disable</option> </select> Enabled <?php } elseif($_COOKIE["nsfw"] == "false") { ?> <select name="nsfw_options" id="nsfw_options"> <option value="true">Enable</option> <option value="false">Disable</option> </select> Disabled <?php } else { ?> <select name="nsfw_options" id="nsfw_options"> <option value="true">Enable</option> <option value="false">Disable</option> </select> Disabled <?php } ?> I meddled around with it but when navigating from the page and back again it goes from enabled to disabled. Helpz. Quote Link to comment https://forums.phpfreaks.com/topic/317286-nsfw-filter-conditionals/#findComment-1611833 Share on other sites More sharing options...
oz11 Posted September 14, 2023 Author Share Posted September 14, 2023 Ah. forgot to clean my cookies. Seems to work okay. Thanks for being here guys, helps alot! Quote Link to comment https://forums.phpfreaks.com/topic/317286-nsfw-filter-conditionals/#findComment-1611834 Share on other sites More sharing options...
oz11 Posted September 14, 2023 Author Share Posted September 14, 2023 (edited) Code in all it's glory. I will leave it here.. <?php if ($_GET["nsfw_options"] == "true") { $sfw = 1; setcookie("nsfw", "true"); $_COOKIE["nsfw"] = "true"; } if ($_GET["nsfw_options"] == "false") { $sfw = 0; setcookie("nsfw", "false"); $_COOKIE["nsfw"] = "false"; } if (!isset($_COOKIE["nsfw"])) { $sfw = 0; setcookie("nsfw", "false"); $_COOKIE["nsfw"] = "false"; } ?> NSFW Filter: <?php if ($_COOKIE["nsfw"] == "true") { ?> <select name="nsfw_options" id="nsfw_options"> <option disabled selected value> -- select -- </option> <option value="false">Disable</option> <option value="true">Enable</option> </select> <small>[Currently enabled]</small> <?php } elseif ($_COOKIE["nsfw"] == "false") { ?> <select name="nsfw_options" id="nsfw_options"> <option disabled selected value> -- select -- </option> <option value="true">Enable</option> <option value="false">Disable</option> </select> <small>[Currently disabled]</small> <?php } else { ?> <select name="nsfw_options" id="nsfw_options"> <option disabled selected value> -- select -- </option> <option value="true">Enable</option> <option value="false">Disable</option> </select> <small>[Currently enabled]</small> <?php } ?> Messy but ok. or a change of markup. (v2).. <?php if ($_GET["nsfw_options"] == "true") { $sfw = 1; setcookie("nsfw", "true"); $_COOKIE["nsfw"] = "true"; } if ($_GET["nsfw_options"] == "false") { $sfw = 0; setcookie("nsfw", "false"); $_COOKIE["nsfw"] = "false"; } if (!isset($_COOKIE["nsfw"])) { $sfw = 0; setcookie("nsfw", "false"); $_COOKIE["nsfw"] = "false"; } ?> NSFW Filter: <?php if ($_COOKIE["nsfw"] == "true") { ?> <select name="nsfw_options" id="nsfw_options"> <option disabled selected value>[Currently enabled]</option> <option value="false">Disable</option> <option value="true">Enable</option> </select> <?php } elseif ($_COOKIE["nsfw"] == "false") { ?> <select name="nsfw_options" id="nsfw_options"> <option disabled selected value>[Currently disabled]</option> <option value="true">Enable</option> <option value="false">Disable</option> </select> <?php } else { ?> <select name="nsfw_options" id="nsfw_options"> <option disabled selected value>[Currently disabled]</option> <option value="true">Enable</option> <option value="false">Disable</option> </select> <?php } ?> Edited September 14, 2023 by oz11 Quote Link to comment https://forums.phpfreaks.com/topic/317286-nsfw-filter-conditionals/#findComment-1611835 Share on other sites More sharing options...
Solution requinix Posted September 14, 2023 Solution Share Posted September 14, 2023 46 minutes ago, oz11 said: Thanks. Is there a workaround? Ps: Glad you said that, I thought I was ging mad (again). If you know that the form was submitted then you could take the absence of a value as proof that it was unchecked. But yes, there is a simple solution that lets you keep checkboxes: use hidden inputs. <input type="hidden" name="checkbox" value="off"> <input type="checkbox" name="checkbox" value="on"> When checked, the checkbox's "on" overwrites the hidden input's "off". 1 Quote Link to comment https://forums.phpfreaks.com/topic/317286-nsfw-filter-conditionals/#findComment-1611836 Share on other sites More sharing options...
oz11 Posted September 14, 2023 Author Share Posted September 14, 2023 48 minutes ago, requinix said: If you know that the form was submitted then you could take the absence of a value as proof that it was unchecked. But yes, there is a simple solution that lets you keep checkboxes: use hidden inputs. <input type="hidden" name="checkbox" value="off"> <input type="checkbox" name="checkbox" value="on"> When checked, the checkbox's "on" overwrites the hidden input's "off". Friend, perfect. Quote Link to comment https://forums.phpfreaks.com/topic/317286-nsfw-filter-conditionals/#findComment-1611841 Share on other sites More sharing options...
Barand Posted September 14, 2023 Share Posted September 14, 2023 Another option is to use the null coalesce operator (??), so if ($_GET["nsfw_options"] == "true") { $sfw = 1; setcookie("nsfw", "true"); $_COOKIE["nsfw"] = "true"; } if ($_GET["nsfw_options"] == "false") { $sfw = 0; setcookie("nsfw", "false"); $_COOKIE["nsfw"] = "false"; } becomes $nsfw = $_GET['nsfw_options'] ?? "false"; // if isset() use its value otherwise set default value ("false") $sfw = $nsfw == "true" ? 1 : 0; setcookie("nsfw", $nsfw); $_COOKIE["nsfw"] = $nsfw; 1 Quote Link to comment https://forums.phpfreaks.com/topic/317286-nsfw-filter-conditionals/#findComment-1611848 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.