condition in onclick

Asked

Viewed 213 times

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 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.

  • What I said above was to describe what your difficulty is and post the form html along with the question.

2 answers

1


So, if I understood correctly what was missing there to validate the field and send the record was to call the function insert in the else, that is, if the field is valid:

var myform = document.forms['formulario'] || document.formulario;

function validarFormulario(){
    if(myform.Tipo.value == "0" || myform.Tipo.selectedIndex == 0){
        alert('Campo Obrigatório!');
    }else{
        inserir_registo()             // se o campo estiver válido insere o registro
    }
}

function inserir_registo() {
  myform.submit()                     // apenas para o exemplo
  
  /* var dadosajax = {
     'Tipo' : $("#Tipo").val(),
     .... 
  */   
}
<form id="formulario" name="formulario" method="POST">
  <select name="Tipo" id="Tipo">
    <option value="0">Vazio</option>
    <option value="1">Teste 1</option>
    <option value="2">Teste 2</option>
  </select>
  <button type="button" onclick="validarFormulario()">Enviar</button>
<form>

  • 1

    That’s just what I needed.

0

For it to be a valid codition, I believe you have to return something on function validarFormulario.

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();
        return true;
    }
}

Then, just check whether this function will return true or not on the button.

<button type="button" data-toggle="modal" class="btn btn-success" onclick="if(validarFormulario() === true) inserir_registo();">Enviar</button>

You can even omit the === true.

<button type="button" data-toggle="modal" class="btn btn-success" onclick="if(validarFormulario()) inserir_registo();">Enviar</button>

  • insert into it at the end of the check

Browser other questions tagged

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