0
I intend to create a onclick
button insert into database.
I have the following duties:
Insert function:
function inserir_registo()
{
var dadosajax = {
'Tipo' : $("#Tipo").val(),
'Prioridade' : $("#Prioridade").val(),
'Email' : $("#Email").is(":checked") ? $("#Email").val() : '',
'Para' : $("#Para").val(),
'Assunto' : $("#Assunto").val(),
'Conteudo' : $("#Conteudo").val(),
'De' : $("#De").val()
};
$.ajax({
url: './inseriralerta',
type: 'POST',
cache: false,
data: dadosajax,
error: function(){
},
success: function(data)
{
$('#formulario')[0].reset();
}
});
}
Function to check field:
function validarFormulario(){
var myform = document.forms['formulario'] || document.formulario;
if(myform.Tipo.value == "0" || myform.Tipo.selectedIndex == 0){
alert('Campo Obrigatório!');
}else{
myform.submit();
}
}
Knob:
<button type="button" data-toggle="modal" class="btn btn-success" onclick="if(validarFormulario() == 'OK') inserir_registo();">Enviar</button>
So the problem is I just want you to perform the function inserir_registo();
in the case of this function validarFormulario();
check that the field is filled, if it is not performing the function inserir_registo();
HTML:
<form id="formulario" name="formulario" method="POST">
<div class="row clearfix">
<div class="col_half1">
<label>Tipo de Alerta</label>
<div class="input_field">
<select name="Tipo" id="Tipo">
<option></option>
<?php
$sql = "SELECT Discricao FROM raddb.TipoAlertas WHERE Id IN ('1','2','7')";
$qr = mysqli_query($conn, $sql);
while($ln = mysqli_fetch_assoc($qr)){
echo '<option value="'.$ln['Discricao'].'">'.$ln['Discricao'].'</option>';
}
?>
</select>
</div>
</div>
<div class="form-group">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancelar</button>
<button type="button" data-toggle="modal" class="btn btn-success" onclick="if(validarFormulario() == 'OK') inserir_registo();">Enviar</button>
</div>
</form>
Failed to say what error or difficulty you have and tbm insert the Html form.
– LeAndrade
@Leandrade the problem is not to insert, because it inserts. The problem is in the onclick of the insert button
onclick="if(validarFormulario() == 'OK') inserir_registo();"
where I only want you to enter in case the field is filled in. As it is it checks that the field is unfilled but inserts it in the same.– Bruno
What I said above was to describe what your difficulty is and post the form html along with the question.
– LeAndrade