Default value to an input in HTML


I have a form that will register your name, surnames, etc. and I have a field that asks for a number, I already register well, now I would like it if the number field is empty that I insert something by default, example: 0.

In the form I get the value of the input like this:

<div class="form-group">
    <label for="name">Número</label>
    <input type="number"  name="no"  class="form-control" placeholder="23" value="<?php if(isset($_POST['no'])) echo $_POST['no'];  else echo "0" ?>" />


    $db_handle = new DBController();

     $username = $_POST['no'];

     $username = '8787';

$query = "INSERT INTO tabla (.....,no_interior,...) VALUES
    (...,'" . $username . "',,...)";

But I do not register anything in the bd.

Thank you.

asked by x4mp73r 11.04.2016 в 18:44

3 answers


The simplest solution:

if (isset($_POST['no'])) {
    $username = $_POST['no'];
else {
    $username = 0;
answered by 13.04.2016 в 11:59

I suggest you check the variable, it may not be empty.

Test with IS_NULL or with "" in your if to first identify what you are getting when the field goes "empty"

answered by 14.04.2016 в 20:26

If you use Mysql you can configure the table so that it inserts default values if no value is assigned to it with the Query.

answered by 15.04.2016 в 09:26