Jump to content

NSFW filter conditionals


oz11
Go to solution Solved by requinix,

Recommended Posts

<?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 by oz11
Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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 by oz11
Link to comment
Share on other sites

  • Solution
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".

  • Great Answer 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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; 

 

  • Great Answer 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.