Hello there,
I'm having some troubles with checkboxes. Everything works fine when i checked the checkbox (the form register correctly the value 1 that is attached for checkbox checked), but when it remains unchecked the form don't pass any value, remains blank.
My form looks like this for add.php

<label><h2>Kodi Shtetit</h2></label>
<input type="text" name="shteteKodi" value="<?=$shteteKodi;?>" style="width:50%;"  />

and for edit.php looks like this:

<label><h3 style="color:green;">Aktiv</h3></label>
		<input id="shteteAktive" name="shteteAktive" type="checkbox" <?php if ($shteteAktive == "1") { echo "checked";} ?> value="1">

The db field type is ENUM and values are 1 and 0

Hope that someone can help me on this. Thank you in advance 😉

    Kleidi;10989188 wrote:

    Everything works fine when i checked the checkbox (the form register correctly the value 1 that is attached for checkbox checked), but when it remains unchecked the form don't pass any value, remains blank.

    That sounds like it's behaving exactly as it's supposed to be behaving, so.. what's the problem?

      The problem is that it does not put the 0 value where is not unchecked 🙁 So, when is checked, the value registered on db is 1; when is unchecked the db field remains null. I need to appear 0 on that case.

        Kleidi;10989218 wrote:

        I need to appear 0 on that case.

        Then you need to handle that case when you're INSERT/UPDATE'ing the data in the DB. If a checkbox is not checked, it won't be included at all in the form data. Thus, if you want a '0' to be used if the value isn't set, then you need to use [man]isset/man or something similar to detect that and substitute a '0' in for the nonexistent POST variable.

          bradgrafelman;10989219 wrote:

          Then you need to handle that case when you're INSERT/UPDATE'ing the data in the DB. If a checkbox is not checked, it won't be included at all in the form data. Thus, if you want a '0' to be used if the value isn't set, then you need to use [man]isset/man or something similar to detect that and substitute a '0' in for the nonexistent POST variable.

          Aham...too complicated for a 0 😛
          I thought that i have made something wrong and the value didn't appear. I didn't knew that this is the way it works.
          Thanks for your fast replies and for helping.
          Have a nice day 😉

            Write a Reply...