Skip to content Skip to sidebar Skip to footer

Validating Input Types "checkbox" And "number" Php/html

Hi i was wondering if it is possible to validate 'checkbox' and 'number'. It is really hard to explain what I want but I'll try my best ;). Anyways I have this code:

Solution 1:

Three words: array it up!

Observe this code:

<formaction="order.php"method="post"><p>
        Coffee: <br><!-- This ensures false is submitted if the cappuccino box is unticked --><inputtype="hidden"name="coffee[cappuccino][selected]"value="0"><label><inputtype="checkbox"name="coffee[cappuccino][selected]"value="1"> Cappuccino</label><inputtype="number"name="coffee[cappuccino][qty]"size="2"><br><!-- This ensures false is submitted if the espresso box is unticked --><inputtype="hidden"name="coffee[espresso][selected]"value="0"><label><inputtype="checkbox"name="coffee[espresso][selected]"value="1"> Espresso</label><inputtype="number"name="coffee[espresso][qty]"size="2"><br></p><p>[...]</p><p><label><inputtype="radio"name="dine"value="in"> Dine in</label><label><inputtype="radio"name="dine"value="out"> Take out</label></p><p><inputtype="submit"value="submit"name="submit"></p></form>

When this submits, the inputs are submitted as an associative array as this:

array (size=3)
  'coffee' => 
    array (size=2)
      'cappuccino' => 
        array (size=2)
          'selected' => string'1' (length=1)
          'qty' => string'4' (length=1)
      'espresso' => 
        array (size=2)
          'selected' => string'1' (length=1)
          'qty' => string'3' (length=1)
  'dine' => string'in' (length=2)
  'submit' => string'submit' (length=6)

As you can see, each coffee is now being submitted as an array with the type of coffee being a key that also has an array of its own with two further keys: selected and qty.

The selected determines if the checkbox for that coffee was ticked (1) or not (0) and the qty holds the user input.

This pretty much does your validation since each quantity input now belongs to the individual coffee.

I hope I've understood your dilemma correctly and this answers your question or give a you an idea of how to go on about doing this.

Enjoy :)

Post a Comment for "Validating Input Types "checkbox" And "number" Php/html"