Save Checkbox Status To Browser?
  • Hey guys, I'm really hoping you can help me out. I've got a bunch of checkbox's on my website and need a simple "save" button to save the status of the box. The box's have no value. Lol, they're just a "what have I finished" checkbox with nothing returning data.

    So how would I make the "save" button so that it saves the states to the user's cache or something?

  • <input type="checkbox" name="joe" />


    Once the form is sent,


    $joe = isset($joe) ? 1 : 0;


    Then you store $joe wherever you want.
  • If you want it all local, use cookies & JavaScript.

    This page does the same thing: http://www.keyframesandcode.com/resources/php/redmine/documentor

  • hey, thanks a lot! I'm wanting it local, so I'm looking at the URL. Would you be able to explain a little to what it's doing? What do I put on my website? The end code it gives you?

    Thanks, I'm new to html...
  • Hmm, well if you're new to HTML you are jumping in at the deep end somewhat.

    The simplest way to access a checkbox is with

    document.form.control.checked = state

    You can loop through form controls too, and save their states to a cookie, so you need to look at the code for saving and loading cookies. Essentially, cookies can save whatever you like, just in this example you're saving and loaded the states of checkboxes.

    If you want to get started with JavaScript, make sure you're using Firefox, download Firebug, and do some interactive testing in the JavaScript console.

    Sorry I'm not giving you the exactcode, but there is a tonne of info out there... just get googling :)

  • OK, I'll take a look at cookies more. I'm just needing something really simple to save the checkbox into a cookie... so I'll start googling now!
  • And no, that page is designed to generate HTML code for documentation, I just gave you the url as it's just that the page saves the checkbox controls' states between page loads.

  • In my opinion, Javascript should be used as an extra layer to enhance the already working PHP/HTML code. The only time I use Javascript only solutions is when I'm building an application like a WYSIWYG editor or something like that.

    As a side note, you should probably post in some HTML or Javascript forums for this kind of help.
  • I'd just do something like this:


    <?php
    if (isset($_POST))
    {
    $checkboxes = $_POST['checkbox];

    setcookie('checkboxes', implode(';', $checkboxes), 3600);
    }

    if (empty($checkboxes))
    {
    $checkboxes = explode(';', $_COOKIE['checkboxes']);
    }
    ?>

    <form method="post" action="">
    <input type="checkbox" name="checkbox[]" value="Task 1"<?php if (isset($checkboxes[0])) echo ' checked="checked"'; ?> />
    <input type="checkbox" name="checkbox[]" value="Task 2"<?php if (isset($checkboxes[1])) echo ' checked="checked"'; ?> />
    ... and so on...
    </form>
  • I'd try to avoid using JavaScript to set/access your cookies. You can do it server side. Use code similar to that posted above to detect which checkboxes are checked, then use PHP to set the cookies accordingly, as part of the HTTP response

    http://www.w3schools.com/PHP/php_cookies.asp

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion