How to activate the Insert only when you click the insert button

Asked

Viewed 96 times

1

I have a form that every time it is given post, it is giving error by lack of parameter. I would like to give Insert only when someone clicks on the insert button (hand on the drawing). I thought to disable the button to enable only when it is filled but do not know how to proceed. Desabilitando Insert

https://gist.github.com/FabricioYwin/2b0483e25b030a0be54037751af78e89

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 from the beginning and end of the string. Then checks if it is empty.

  • but how do I exbible that alert? Can I use bootstrap Alert?

  • Yeah, you can use it. I do so: if (isset($_POST['variavel_x'])){ /if the variable exists, that is if there is any content, if it was filled, or in the case of the button, if it was clicked; }

  • You can. On the page you check if there is a variable $error. If yes, you show.

Browser other questions tagged

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