I am trying to incorporate a checkbox into a form that adds a record to an sql table called 'contracts'

The table has a field called 'override' which is either NULL or will contain a '1'.

I want the form to have a checkbox to select the override option, and when the contract is submitted, it submits a '1' to that field if the box is checked. If it's not checked, it just ignores it.

At the moment, I have:

<input type=\"checkbox\" name=\"override\">

But I'm unsure what values to take as the POST values etc. Can anyone help?

    Checkboxes are either set (checked) or not set (unchecked). They take whatever value you give them, just like other input elements do. It's only inputs of type text and password that provide an interface to the user for modifying the value, but wether the user can change the possible values of an input or not doesn't matter. If you want the value 1, specify that.

      johanafm;10990306 wrote:

      Checkboxes are either set (checked) or not set (unchecked). They take whatever value you give them, just like other input elements do. It's only inputs of type text and password that provide an interface to the user for modifying the value, but wether the user can change the possible values of an input or not doesn't matter. If you want the value 1, specify that.

      This is the bit I'm having trouble understanding I think. By default, I'd like the box to be unchecked, however leaving the user with the option of changing that to checked before they submit the form.

      Now, if I use this line for the checkbox in the form:

      <input type=\"checkbox\" name=\"override\" value=\"1\">

      How do I ensure that it only takes the value of '1' forward as a POST value for 'override' if it's checked? In other words, if the box is unchecked when the form is submitted, I want it to assume there is no value.

        Use [man]isset/man to check if $_POST['override'] is set or not (and simply ignore its actual value if it is).

          if the box is unchecked when the form is submitted, I want it to assume there is no value.

          A checkbox will not show up in the $POST array unless it is checked when the form is submitted. This means that $POST['checkbox'] will only be set (using the isset brad mentioned) if it was checked.

            Write a Reply...