ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to send a value for an UNCHECKED checkbox?

August 06, 2009 9:00pm

Subscribe [3]
  • #1 / Aug 06, 2009 9:00pm

    Mark Bowen

    12637 posts

    Hiya,

    Trying to do something clever today but have come across a slight problem.

    I would like to use a checkbox to send one value when it is checked yet to send another value when it isn’t checked.

    Now as checkbox input types can only have the one value and by default if you don’t check it then nothing gets sent then is there any way to send a different value (I’m guessing Javascript will be needed?) when it isn’t checked?

    Thanks for any help on this.

    Best wishes,

    Mark

  • #2 / Aug 07, 2009 1:56am

    This seems to work, unless I misunderstand you question…

    {if foo == ""}
    
    {assign_variable:foo="bar"}
    
    {/if}
    
    
    {foo}
    
    displays "bar"
  • #3 / Aug 07, 2009 5:04am

    Mark Bowen

    12637 posts

    Hiya,

    What I mean is say you have this in a page :

    <input type="checkbox" value="Yes" name="field_id_12" id="field_id_12" />

    If you were to have the checkbox ticked and submit the form then the Yes value would be sent. That’s all fine but what I want is for if the checkbox isn’t ticked for a different value to get sent with the form instead.

    Normally if a checkbox isn’t ticked then no value at all gets sent and so whatever value has already been saved to that field doesn’t get changed at all.

    If you imagine that there is a weblog entry where that field has already had its value set to Yes and you want to edit the field to now say No by not ticking the checkbox, that’s what I want to do.

    Problem being though that if you don’t tick a checkbox then nothing at all gets sent and so the field doesn’t get written over.

    I need this because when you are inputting data the first time then a checkbox is a great way to do this but when you come to editing data then a checkbox isn’t cutting it as there is just no way (at the moment until someone clever in here comes up with the javascript for me 😉 ) to change that data using a checkbox.

    Seems weird to me that checkboxes are made in this way, I don’t see why they can’t have two values, one for checked and one for not checked?

    Anyway I was thinking that there might be some Javascript which would basically say if this checkbox isn’t ticked then set the value of the checkbox to No but if it is ticked then use the value which is already set in the input parameter which is Yes.

    See, clear as mud 😉

    Any help on this would be massively appreciated.

    Best wishes,

    Mark

  • #4 / Aug 07, 2009 5:18am

    Ingmar

    29245 posts

    That’s all fine but what I want is for if the checkbox isn’t ticked for a different value to get sent with the form instead.

    What you want is a radio button, I think…

  • #5 / Aug 07, 2009 9:50am

    ender

    1644 posts

    What you want is a radio button, I think…

    exactly what I was going to say

    however…

    until someone clever in here comes up with the javascript for me

    since I’ve been challenged here… maybe something like this: 😉

    [removed]
        $(document).ready(function() {
            $("#field_id_whatever_assistant").click(function() {
                if($("#field_id_whatever_assistant:checked").val() !== null) {
                    $("#field_id_whatever").val("checked_val");
                } else {
                    $("#field_id_whatever").val("unchecked_val");
                }
            });
        });
    [removed]
    
    <input type="hidden" name="field_id_whatever" value="current_value">
    
    <input type="checkbox" id="field_id_whatever_assistant" {if supposed_to_be_checked}checked{/if}>
  • #6 / Aug 07, 2009 10:05am

    Mark Bowen

    12637 posts

    What you want is a radio button, I think…

    exactly what I was going to say

    Actually nope I don’t 😉

    I know it seems very very crazy what I’m doing but there is a very good reason for it 😊

    I’m wanting to use this script here as an easy way for people to set a value and also change that value at a later date. Unfortunately as that script works with a checkbox I’m not able to send more than one value which is what I really need.

    I find it a little strange actually that say you have a form for entering some details and you have a load of checkboxes where you can say yes to certain things. If you now had an edit form how would you then allow people to uncheck a checkbox and still have the value that is saved change? Does that make more sense perhaps?

    however…

    until someone clever in here comes up with the javascript for me

    since I’ve been challenged here… maybe something like this: 😉

    [removed]
        $(document).ready(function() {
            $("#field_id_whatever_assistant").click(function() {
                if($("#field_id_whatever_assistant:checked").val() !== null) {
                    $("#field_id_whatever").val("checked_val");
                } else {
                    $("#field_id_whatever").val("unchecked_val");
                }
            });
        });
    [removed]
    
    <input type="hidden" name="field_id_whatever" value="current_value">
    
    <input type="checkbox" id="field_id_whatever_assistant" {if supposed_to_be_checked}checked{/if}>

    Thanks will give that a go as soon as I can.

    I’m thinking now though that as I only need to do this for one checkbox that I may just write an extension that basically will just check for a missing value for that field and if it is indeed missing (i.e. not checked) then I will change the value programatically using the extension instead.

    Will definitely give that code a go though, thanks ender.

    Best wishes,

    Mark

  • #7 / Aug 07, 2009 10:18am

    ender

    1644 posts

    knowing what you’re after now, maybe a better way would be to use the submit event to modify the checkboxes…

    [removed]
        $(document).ready(function() {
            $("#SAEF").submit(function() {
                $("#SAEF input[type=checkbox]:not(:checked)").each(function() {
                    $(this).val("no");
                    $(this).attr("checked", true);
                });
            });
        });
    [removed]
    
    <form id="SAEF" ...>
    ...
    </form>
  • #8 / Aug 07, 2009 10:26am

    Mark Bowen

    12637 posts

    Hmm just tried the code and whilst I’m not entirely certain it was doing what I think it should have done I also don’t think that it’s going to be as easy as I originally thought as that javascript doesn’t seem to fire when I’m using the iPhone style checkbox script.

    I’m thinking that an extension will be best for me to create here. It’s also quite an important checkbox so if people don’t have javascript enabled then I would need to be doing some server-side checking anyway.

    Thanks for the help though ender as that code has gone in my head as to how I would do this if I wasn’t using that other script too.

    Best wishes,

    Mark

  • #9 / Aug 07, 2009 10:30am

    ender

    1644 posts

    my second attempt should work better with your special checkboxes (the way that plugin works is that it hides the actual checkbox and builds its own thing, so the checkbox never gets an actual click)... but if you need to make sure that it works for everyone even with JS disabled then you’ll definitely be doing something server-side anyway, as you said.

  • #10 / Aug 07, 2009 10:31am

    Mark Bowen

    12637 posts

    Hiya,

    knowing what you’re after now, maybe a better way would be to use the submit event to modify the checkboxes…

    Thanks for the second piece of code although again I don’t think it’s working either in conjunction with the other script or even without that script in the mix.

    Not too sure what I’m doing wrong though. I changed the SAEF bit to entryform as that’s the id that is given to SAEF forms although it didn’t seem to make any difference.

    I think perhaps an extension will be the best bet on this one maybe.

    Thanks again for all the help it’s much appreciated.

    Best wishes,

    Mark

  • #11 / Aug 07, 2009 10:56am

    Mark Bowen

    12637 posts

    Just as an aside ender (as you seem to always understand what I’m babbling on about 😉 ) I have now made an extension which checks for the existence of that checkbox. Basically at the moment all I am doing is a :

    echo "<pre>";
    print_r($_POST);
    echo "</pre><p>“;
    </pre>

    when the SAEF is submitted so that I can see whether or not the value is there. If I don’t use the iPhone checkbox script then the value appears if the box is checked and doesn’t appear if it isn’t checked which is what I expected to have happen.

    However if I place the iPhone script into the equation I never ever get to see a value submitted no matter if the ‘checkbox’ is checked or not. Should the iPhone script not just be an over the top visual thing and still submit the value?

    Seems like I’m just not going to get this working now :-(

    Best wishes,

    Mark

  • #12 / Aug 07, 2009 11:03am

    ender

    1644 posts

    yeah the way the guy described it, it hides the checkbox and manipulates it in the background with the custom slider widget thing, so your server shouldn’t know the difference… I’m surprised it’s not showing up, unless the javascript is broken somehow.  got a test page I can look at?

  • #13 / Aug 07, 2009 11:11am

    Mark Bowen

    12637 posts

    yeah the way the guy described it, it hides the checkbox and manipulates it in the background with the custom slider widget thing, so your server shouldn’t know the difference… I’m surprised it’s not showing up, unless the javascript is broken somehow.  got a test page I can look at?

    Unfortunately testing this on a localhost so no login I’m afraid :-(

    Nevermind I will soldier on and bring down a really really simplified version of a form, perhaps not even an SAEF and just see if the value is sent through or not.

  • #14 / Aug 07, 2009 11:42am

    Mark Bowen

    12637 posts

    Hmm very very weird.

    If I create a form myself and send the action=”“ to a template which has some PHP in it which just outputs all the $_POST data then it seems as though the checkbox does indeed send a value when checked.

    I then tried it within my Stand-Alone Edit Form and got rid of absolutely everything else within the form except for the checkbox, sent it to the same template with the PHP on it to output the $_POST data and no value!! :-(

    Very very weird! Back to the drawing board on this one methinks!! :-(

  • #15 / Aug 07, 2009 11:56am

    ender

    1644 posts

    could try throwing your print_r($_POST) into cp.publish.php in the submit_new_entry function to see exactly what is making it to the publish code… I know that some posted variables get unset along the way, though your custom fields shouldn’t get touched I don’t think.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases