0

My php form validation is OK. I want to keep user entered data in the form when it is represented to the user (i.e., if an error occurs). I can do with for text entries using this code:

    <input type="text" name="firstname" id ="firstname" value = "<?php echo $firstname;?>"      > 

And for radio buttons using this code:

    <input type="radio" name="entree" <?php if (isset($entree) && $entree =="chicken") echo "checked";?> value = "chicken">Chicken 

But I can't get it to work for decimal inputs, using the following code.

     <input type="DECIMAL(4,2)" name = "meal_cost" id ="meal_cost"  value = "<?php echo    htmlspecialchars($meal_cost);?>"> 

I will appreciate any help from Stackoverflow

lighter
  • 2,808
  • 3
  • 40
  • 59
user3307589
  • 27
  • 1
  • 4
  • possible duplicate of [How to add maxlength for HTML5 input type="number" element?](http://stackoverflow.com/questions/8354975/how-to-add-maxlength-for-html5-input-type-number-element) – lighter May 30 '14 at 23:06

1 Answers1

0

There is no 'Decimal(4,2)' input type in html. Just use <input type="number"... />

You can set a max and a min value on the input by using max and min attributes. Checkout the spec on input number types: http://www.w3.org/TR/html-markup/input.number.html

Also you can use the php number_format function to format the values when you are rendering the form and limit it to 2 decimals. That doesn't mean the user will submit only 2 decimals. You would need to add something to verify / correct values at they are posted to the server.

Putting it all together, you should change

<input type="DECIMAL(4,2)" name = "meal_cost" id ="meal_cost"  value = "<?php echo    htmlspecialchars($meal_cost);?>">

To

<input type="number" max="9999" min="0" step="any" name="meal_cost" id="meal_cost" value="<?php echo number_format($meal_cost,2);?>">
dmullings
  • 7,070
  • 5
  • 28
  • 28