Combining checkbox and button

Discussion in 'JavaScript' started by PoPSiCLe, Mar 19, 2009.

  1. #1
    Hey.

    I currently have this form:

    
    <form id="adminchoice" method="post" action="<?php echo $domain; ?>/session_variable.php">
    	<div>
    		<label class="login">Hak av for å vise adminverktøy</label>
    		<input class="right" type="submit" value="Admin on/off" name="admintools" />
    		<input class="right" type="checkbox" name="admincheck" <?php if ($_SESSION['hide_tools'] == "yes") { echo "checked='checked'"; } elseif ($_SESSION['hide_tools'] == "") {} ?> />
    	</div>				
    </form>
    
    PHP:
    It works just like I want it to work, but I feel it is a hassle to always have to click the checkbox to either remove or add a checkmark, and then having to click the button to update.

    I'd like a way for doing the update with just the button - but alas, js is still not my forté, and I can't seem to figure out how to make a value pass through to the session_variables.php-file that will make it possible to validate it and return a $_SESSION.

    The session_variable.php file looks like this:
    
    <?php
    session_start();
    if (isset($_POST["admincheck"])) {
    	$_SESSION['hide_tools']="yes"; }
    else { $_SESSION['hide_tools']=""; }
    header("location: $_SERVER[HTTP_REFERER]");
    ?>
    
    PHP:
    So... can anyone rewrite this, or tell me what I need to do to get it to work with just the button?

    What I want, in short, is that if I press the button while the session is set to "yes", it turns to no/empty, and if I press it while the session is empty/set to "no", it will change to "yes".

    Anyone?
     
    PoPSiCLe, Mar 19, 2009 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi,
    I'm still not sure what exactly you mean. DO you want to switch session on/off after form submit without using checkbox?? Then you could use hidden form field :
    <form id="adminchoice" method="post" action="<?php echo $domain; ?>/session_variable.php">
    <div>
    <label class="login">Hak av for å vise adminverktøy</label>
    <input class="right" type="submit" value="Admin on/off" name="admintools" />
    <input type="hidden" name="admincheck" value="<?php echo $_SESSION['hide_tools'] ?>" />
    </div>
    </form>

    And in PHP (assuming default is "yes"):
    <?php
    session_start();
    if (isset($_POST["admincheck"])) {
    if($_POST["admincheck"]=="yes") $_SESSION['hide_tools']="no";
    else $_SESSION['hide_tools']="yes";
    }else $_SESSION['hide_tools']="yes";
    header("location: $_SERVER[HTTP_REFERER]");
    ?>

    Hope it helps someway....
     
    lp1051, Mar 20, 2009 IP
    PoPSiCLe likes this.
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Duh... I feel extremely stupid now. Thanks, it works perfectly. :D
     
    PoPSiCLe, Mar 20, 2009 IP