Call form send by function

1

I'm trying to send a form through a function. Follow the structure.

form with id: last name
input with id: nickname
event keyup no input.

onkeyup="$(this).check()"

Javascript

(function($) {
        $.fn.check = function() {
            alert('a');
             $('#apelido').delay(200).submit();
        
        };
    })(jQuery);

    $("#apelido").submit(function (event) {  

        alert('s');     
        event.preventDefault(); 
        $.ajax({       
        type: "get",
        url: '<?php echo base_url();?>index/check',
        data: {id: '425', inputv : 'adm', nameinput: 'apelido'},
        success: function (response) {
                alert(response);
                      }
        });
    });
   

The first alert is called, the second is not ... I no longer know what to do. Any help is welcome.

    
asked by anonymous 24.06.2016 / 01:25

2 answers

1

Hello. Apparently you have two identical id's on the same page, which is invalid by specification, even if it refers to different tags.

You can change the form id to something like form-apelido and input to input-apelido and make changes to the jquery selector.

A suggestion:

HTML

<form id="form-apelido">
<input id="input-apelido" onkeyup="$(this).check()" />
</form>

JAVASCRIPT

(function($) {
        $.fn.check = function() {
            alert('a');
             $('#form-apelido').delay(200).submit();

        };
    })(jQuery);

    $("#form-apelido").submit(function (event) {  

        alert('s');     
        event.preventDefault(); 
        $.ajax({       
        type: "get",
        url: '<?php echo base_url();?>index/check',
        data: {id: '425', inputv : 'adm', nameinput: 'apelido'},
        success: function (response) {
                alert(response);
                      }
        });
    });

FIDDLE with code working: link

    
24.06.2016 / 03:37
0

Issue: Bypassing Codeigniter's deficiency in the is_unique issue for update, since it does not check if the field being updated is from the same user, thus preventing it from being used in the update form. Solution: Form with user data obtained from DB. I just put the field to search for clarity, but consider several fields like, tel, cel, cep etc.

<form action="algumlugarproupdate.php" method="post">
<input type="text" id="pesquisa" onkeyup="$(this).check($(this).val())">
</form>

Ghost form:

 <form id="test" method="get">
 </form>

JavaScript Snippet

<script type="text/javascript">    
  var field1;
  (function($) {
  $.fn.check = function(data1) {
   //Aqui eu passo os valores da função para variáveis globais
   //Fica mais fácil de manipular fora deste escopo
   field1=data1;  
   //Aqui envio o form "fantasma" com delay de 0.2 seg       
 $('#test').delay(200).submit();        
};
})(jQuery);
 //Aqui intercepto o submit
$("#test").submit(function (event) {  
event.preventDefault(); 
$.ajax({       
type: "get",
url: 'retorno_true_false.php',
data: {login: field2},
    //pego o que foi retornado do retorno_true_false.php
    success: function (data) {
    //retiro possíveis espaços
    data.trim();
    if (data>0) {
        alert("Ops, este login já existe");
        //volto o campo pesquisado para o valor original
        $("#pesquisa").val($("#pesquisa").attr("value"));
        }
    }
});
})(jQuery);

I think that's it!

    
24.06.2016 / 15:36