Set default input value without appearing to the user

Asked

Viewed 769 times

0

I wonder if it is possible for me to define a default value that would be number 0 without this 0 being shown to the user, this value is necessary because I am taking these numbers typed and processing in PHP, I store in a variable, and multiply the values, when I leave one of these empty inputs PHP displays an error that says the value is not a precise number that this number 0 is hidden to the user and works under the screens

<html>
<head></head>
<body>
  <input type="number" value="0">
</body>
</html>

  • 1

    creates an attribute for this: <input type="number" default-value="0"> but remember nothing in hmtl gets even no access to the user, just view the page code

  • Now it gave error in my PHP, the error says it is a value non-numeric value encountered, a value not numerical I used it for sum so it shows this error, the situation is simple but there is something behind, IF I do not leave the input empty using the number 0 as value it does not generate number error, if I use this attribute it generates this error

  • An answer using JS would help you?

  • yes, it can even be in css if you have

  • What do you mean when you quote that this value is not sample to the user?

  • You know that 0? that appears in the place of input? the user will have to delete it manually, or if he does not pay attention will write for example: "0100, 0200" why 0 ta there, nothing else just want that 0 is available for PHP not to give error, would like (if there is) a solution like the google search bar that when clicking can overwrite and so on, or you would know a solution for PHP if the input does not contain any numbers received it uses the 0 in the variable? main is not html the problem is how PHP will process...

  • 5

    @Danielelias, because you leave the value with nothing and when you pass it to PHP it eliminates these inputs with value 0 or simply adds the 0 to the empty inputs?

  • 3

    This type of checking should be done on the server, with PHP. Anything you do, whether with HTML or JS, will be gambiarra and have side effects. Just don’t set the attribute value field and in PHP make the condition if empty, assign 0.

Show 3 more comments

2 answers

2


The placeholder was used to display the number without the user being able to interact with it:

<input type="number" name="numero" placeholder="0" />

In PHP use the function Empty it checks if the value is empty. You can check with an if to do the treatment.

<?php
if (empty($_POST['numero'])) {
    // Trata caso o número que não foi informado
    $numero = 0;
}

-2

If you don’t want to show to the user use:

 <input type="hidden" id="vlalgumacoisa" value="0" />

In Php you take the value of this input quietly, it just doesn’t show up in the form.

  • You’re wrong. AP wants a <input type='number'> that upon being submitted empty, pass a default value to the HTTP method responsible for sending the data. Users are explaining that one should not rely on values coming from the client and that this type of validation should be done on the server

Browser other questions tagged

You are not signed in. Login or sign up in order to post.