How to trigger the insert only when you click the insert button

1

I have a form that every time it is given post, it is giving error for lack of parameter. I'd like to give insert only when someone clicks the insert button (hand in the drawing). I thought about disabling the button to enable it only when it is populated but I do not know how to proceed.   

link

    
asked by anonymous 20.12.2016 / 17:54

1 answer

3

You need to validate the fields.

You can do this in JavaScript, PHP, or both.

For example in PHP:

if(trim($_POST['quantidade']) == ''){
    $error[] = 'Quantidade - Campo Obrigatório!';
}

if(trim($_POST['custo']) == ''){
    $error[] = 'Custo - Campo Obrigatório!';
}

if(trim($_POST['modelo']) == ''){
    $error[] = 'Modelo - Campo Obrigatório!';
}

if(count($error) > 0){
   # Volta para a página sem dar Insert
}
else{
   # Faz insert
}

And so with the other fields.

The trim() takes the spaces at the beginning and end of the string. Then check to see if it is empty.

    
20.12.2016 / 18:00